MySQL COUNT UNION ALL 语句的结果

发布于 2024-12-15 09:04:00 字数 658 浏览 1 评论 0原文

我确信一定有办法做到这一点,但我的 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 技术交流群。

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

发布评论

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

评论(3

蓝眼睛不忧郁 2024-12-22 09:04:00

如果我理解正确的话,应该这样做:

SELECT page_id, count(*)
FROM tags
WHERE tag IN ('New Zealand', 'Trekking')
GROUP BY page_id
HAVING count(*) > 1

如果从同一个表中选择,则不需要使用 UNION 。

If I understood you correctly, this should do it:

SELECT page_id, count(*)
FROM tags
WHERE tag IN ('New Zealand', 'Trekking')
GROUP BY page_id
HAVING count(*) > 1

You don't need to use a UNION if you select from the same table.

掩于岁月 2024-12-22 09:04:00

考虑到您希望 page_id 同时具有 tag "New Zealand""Trekking"


SELECT t1.page_id
FROM tags t1, tags t2
WHERE t1.page_id = t2.page_id
AND
t1.tag="New Zealand"
AND
t2.tag="Trekking"


似乎是正确的,嗯,但检查一次...,如果我发现更容易和更准确,会回复

Considering that you want page_id that have both tag "New Zealand" and "Trekking"


SELECT t1.page_id
FROM tags t1, tags t2
WHERE t1.page_id = t2.page_id
AND
t1.tag="New Zealand"
AND
t2.tag="Trekking"


Seems to be Right, hmm but check once..., will ping back if i found easier and more accurate

偏闹i 2024-12-22 09:04:00

试试这个

CREATE TABLE temporary
SELECT page_id FROM tags
WHERE tag = "New Zealand"
UNION ALL
SELECT page_id FROM tags
WHERE tag = "Trekking"

然后再试试这个

SELECT COUNT(*) FROM temporary

别忘了

DROP TABLE temporary

try this

CREATE TABLE temporary
SELECT page_id FROM tags
WHERE tag = "New Zealand"
UNION ALL
SELECT page_id FROM tags
WHERE tag = "Trekking"

then try this

SELECT COUNT(*) FROM temporary

don't forget to

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