根据另一列中的值从一列中选择项目。

时间:2022-04-12 01:32:15

I have the following data:

我有以下数据:

id1,id2
1    3
1    8
1    10
1    11
2    3
2    10
2    11
3    2
3    18
3    20
4    3
4    8
5    3
5    10
5    11
5    40
5    45
5    50
6    1
6    59
6    70

I won't get all id1 with id2 = 3,10,11.

我不会得到id2 = 3,10,11的所有id1。

For example, id1=4 only with id2=3, should not return.

例如,id1 = 4只有id2 = 3,不应该返回。

The results should be

结果应该是

id1
1
2
5

2 个解决方案

#1


SELECT distinct(ID1) FROM TBTEST WHERE ID2 IN(3,10,11) 

#2


SQL code

SELECT ID1,COUNT(ID2) FROM TBTEST 
WHERE ID2 IN(3,10,11) 
GROUP BY ID1 
HAVING COUNT(ID2)=3

Is this what you need?

这是你需要的吗?

#1


SELECT distinct(ID1) FROM TBTEST WHERE ID2 IN(3,10,11) 

#2


SQL code

SELECT ID1,COUNT(ID2) FROM TBTEST 
WHERE ID2 IN(3,10,11) 
GROUP BY ID1 
HAVING COUNT(ID2)=3

Is this what you need?

这是你需要的吗?