MySQL COUNT UNION ALL 语句的结果
我确信一定有办法做到这一点,但我的 MySQL 知识阻碍了我。
我有一个存储页面标签的表,
page_id tag
51 New Zealand
51 Trekking
58 UK
77 New Zealand
77 Trekking
89 City Break
101 Shopping
...
我想搜索具有两个标签的页面,例如“新西兰”和“徒步旅行”。我查看了 UNIONS、INTERSECT(等效)、JOINS,但我无法找出最好的方法。我想出的最好办法是:
SELECT page_id FROM tags
WHERE tag = "New Zealand"
UNION ALL
SELECT page_id FROM tags
WHERE tag = "Trekking"
... and then some kind of COUNT on those pages that feature twice??
我本质上想将两个搜索合并在一起并“保留”重复项并丢弃其余项。这是否可以通过简单的方式实现,或者我是否需要开始变得复杂?
I'm sure there must be a way to do this but my MySQL knowledge is holding me back.
I have a single table that stores page tags
page_id tag
51 New Zealand
51 Trekking
58 UK
77 New Zealand
77 Trekking
89 City Break
101 Shopping
...
I want to do a search for pages that have two tags, e.g. "New Zealand" and "Trekking". I've looked at UNIONS, INTERSECT (equiv), JOINS and I can't work out what is the best way to do it. The best I have come up with is to do:
SELECT page_id FROM tags
WHERE tag = "New Zealand"
UNION ALL
SELECT page_id FROM tags
WHERE tag = "Trekking"
... and then some kind of COUNT on those pages that feature twice??
I essentially want to UNION the two searches together and 'keep' the duplicates and discard the rest. Is this possible in a simple way or do I need to start getting complex with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,应该这样做:
如果从同一个表中选择,则不需要使用 UNION 。
If I understood you correctly, this should do it:
You don't need to use a UNION if you select from the same table.
试试这个
然后再试试这个
别忘了
try this
then try this
don't forget to