mysql 统计不重叠的 UNION 结果

发布于 2024-12-23 02:59:52 字数 1016 浏览 1 评论 0原文

好吧,我有查询

(SELECT * FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT * FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT * FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
 LIMIT 0, 30

,我将使用 COUNT(*) 运行相同的操作,没有限制

(SELECT COUNT(*) FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT COUNT(*) FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT COUNT(*) FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)

本质上我想做的是获取总结果的计数(没有限制),并且获取有限列表。

我遇到的问题是,当我运行计数查询时,我得到的数字比返回的结果更大。我猜测这样做的原因是当我得到结果时,它会过滤掉所有重叠的行,因为计数只计算每个行,因为我必须手动添加它们。

关于如何清理整个过程有什么想法吗?

另外,请注意,选择语句的数量会动态变化,但我希望有一种方法可以获得重复项的一般计数。

谢谢。

Alright so I have the query

(SELECT * FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT * FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT * FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
 LIMIT 0, 30

and I will run the same thing using COUNT(*) with no limit

(SELECT COUNT(*) FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT COUNT(*) FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)
UNION
(SELECT COUNT(*) FROM jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) ORDER BY ups DESC,downs ASC)

Essentially what I would like to do is get the count of total results(without the limit), and also get the limited list.

The problem I am having is that when I run the count query, I'm getting a bigger number than the results I get back. The reason for this I'm guessing is when I get the results back, it filters out all the overlapping rows, where as the count only counts each, as I manually have to add them.

Any ideas on how to clean up this whole process?

Also, please note that the number of select statements changes dynamically, but I was hoping there was a way to get a general count that accounts for duplicates.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2024-12-30 02:59:52

对于计数来说,这怎么样:

SELECT COUNT(DISTINCT Id)
FROM (SELECT Id From jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) 
      UNION
      SELECT Id From jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) 
      UNION
      SELECT Id From jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?)) t

这应该为您提供唯一的计数,并且还请注意,我删除了 order by,因为它不值得为计数查询进行计算。

How about this for the Count:

SELECT COUNT(DISTINCT Id)
FROM (SELECT Id From jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) 
      UNION
      SELECT Id From jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?) 
      UNION
      SELECT Id From jokes WHERE flags < 5 AND (title LIKE ? OR joke LIKE ?)) t

This should give you the unique count, and also notice that I got rid of the order by because it's not worth calculating for a count query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文