[英]SQL query to match multiple column values
I am having trouble getting my head around the following problem. 我无法解决以下问题。
Given the following table structure and data, how might I select records that match two tags. 给定以下表结构和数据,我如何选择与两个标签匹配的记录。 For example: 例如:
+-----------------+------------------+
| collection_id | tag |
+-----------------+------------------+
| 1 | advertising |
| 1 | tutorials |
| 2 | advertising |
| 2 | coding |
+-----------------+------------------+
If I search for advertising && tutorials, it should return collection_id
= 1, and not collection_id
= 2. 如果我搜索广告&&教程,则应返回collection_id
= 1,而不是collection_id
= 2。
Any pointers most welcome. 任何指针最欢迎。
SELECT collection_ID
FROM tableName
WHERE tag IN ('advertising','tutorials')
GROUP BY collection_ID
HAVING COUNT(*) = 2
If unique
constraint was not specified on the tag for each collection_ID
如果未在标签上为每个collection_ID
指定unique
约束
SELECT collection_ID
FROM tableName
WHERE tag IN ('advertising','tutorials')
GROUP BY collection_ID
HAVING COUNT(DISTINCT tag) = 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.