[英]How can i achieve this with SQL?
Let's say i have a single table called mytable with the following data :假设我有一个名为 mytable 的表,其中包含以下数据:
https://i.stack.imgur.com/hvLHO.png https://i.stack.imgur.com/hvLHO.png
i'm interested in having IDs who belong to both categories A and B我感兴趣的是谁属于A类和B都具有标识
The desired result would be like this :想要的结果是这样的:
https://i.stack.imgur.com/nPmW1.png https://i.stack.imgur.com/nPmW1.png
i tried :我试过 :
select * from mytable where category in ('A','B');
but it doesn't seem to work但它似乎不起作用
You can use exists
:您可以使用
exists
:
select t.*
from mytable t
where
category in ('A', 'B')
and exists (
select 1
from mytable t1
where t1.id = t.id and t1.category in ('A', 'B') and t1.category <> t.category
)
Another approach is a window count (assuming no duplicate in (id, category)
tuples):另一种方法是窗口计数(假设
(id, category)
元组中没有重复(id, category)
):
select id, price, category
from (
select t.*, count(*) over(partition by id) cnt
from mytable t
where category in ('A', 'B')
) t
where cnt > 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.