该选择语句:
select * from favoritetags where tagid = 4
退货
ID |tagid | favoriteid |
12 |4 |12 |
50 |4 |42 |
42 |4 |34 |
同样的声明:
select * from favoritetags where tagid = 29
返回值:
ID |tagid | favoriteid |
49 |29 |41 |
51 |29 |34 |
我只想获取两个列表中最喜欢的列表。
然后,我想对此进行概括,在这里我只能获得那些具有faviriteid与tagid列表共有的行中的内容。其中
(29,4,6)
中的tagid或任意数量的值。
最佳答案
您可以使用聚合:
select favoriteid
from favoritetags
where tag_id in (4, 29)
group by favoriteid
having count(distinct tag_id) = 2
where
子句过滤属于心愿单的tag_id
。该查询按favoriteid
进行汇总,并确保每个favoriteid
具有两个不同的tag_id
(这实际上意味着4
和29
都可用)。这可以轻松扩展到更多
tag_id
:select favoriteid
from favoritetags
where tag_id in (4, 29, 6)
group by favoriteid
having count(distinct tag_id) = 3
关于sql - 多重选择中存在的sql select列。选择与列表中的所有值匹配的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018771/