mysql 统计不重叠的 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)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于计数来说,这怎么样:
这应该为您提供唯一的计数,并且还请注意,我删除了 order by,因为它不值得为计数查询进行计算。
How about this for the Count:
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.