![](/img/trans.png)
[英]In SQL, can I get the values in one column that don't have a certain value associated to them in another column?
[英]how to choose value in 1 column that must have 2 values in another - sql?
桌子:
id lesson
11 11 A
8 11 B
4 11 A
4 11 A
2 11 A
6 11 A
5 11 A
13 11 A
11 11 B
id 11 曾在 11A、11B 教室任教。 如何 select 具有兩個值 11a,11b 的 id?
我試過這個沒有運氣:
select id from table where lesson in '11A' and lesson in '11B'
因為它給出了空表,因為它不能同時是 11a 和 b。
獲取課程的 id
如果可以嘗試對所涉及的 id 和計數使用子查詢,如果 id 有更多則結果
select id
FROM (
select id
from my_table
where lesson in ('11A', '11b')
) t
group by id
having count(*) = 2
如果恰好是兩個值,則可以進行內部聯接
select a.id
from myTable a
inner join myTable b
on a.id = b.id
and a.lesson = '11 A' and b.lesson = '11 B'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.