通用SQL问题

use*_*544 0 sql

我有一张桌子A.

ID    Term
10    A
10    B
10    C
20    A
20    B
20    E
Run Code Online (Sandbox Code Playgroud)

什么是编写SQL的最佳方法

  • 得到ID 10,如果我试图找到(A,B,C)
  • 如果我试图找到(A,B),请不要
  • 得到ID 20,如果我试图在(C,D)中找不到

.

Select distinct ID from TableA where Term in (A,B,C) will return both 10 and 20

Select distinct ID from TableA where Term in (A,B) will also return both 10 and 20

Select distinct ID from TableA where Term NOT in (C,D) will also return both 10 and 20
Run Code Online (Sandbox Code Playgroud)

谢谢!

cod*_*gic 6

1.

SELECT ID 
FROM TableA
WHERE Term IN ('A','B','C')
GROUP BY ID
HAVING COUNT(ID)=3
LIMIT 1
Run Code Online (Sandbox Code Playgroud)

这里3是集合的长度(在这种情况下为A,B,C).2和3可能是上述的一些变化.