[英]How can I select id of rows that all of rows have
There is a table: 这里有张桌子:
+-------------+----------+
|stud_group_id|subject_id|
+-------------+----------+
| 1g | 1s |
+-------------+----------+
| 1g | 2s |
+-------------+----------+
| 1g | 3s |
+-------------+----------+
| 2g | 1s |
+-------------+----------+
| 2g | 2s |
+-------------+----------+
| 3g | 1s |
+-------------+----------+
| 3g | 2s |
+-------------+----------+
| 3g | 4s |
+-------------+----------+
I need to select subject_id
learned by all stud_group_id
. 我需要选择所有
stud_group_id
学到的subject_id
。 I expect 我预计
+----------+
|subject_id|
+----------+
| 1s |
+----------+
| 2s |
+----------+
How can I do this? 我怎样才能做到这一点? Thanks
谢谢
Try this one: 试试这个:
select
subject_id,
count(distinct stud_group_id) as cnt
from
<your_table>
group by
subject_id
having cnt=(select count(distinct stud_group_id) from <your table>)
Something along the lines: 大致情况:
select count(stud_group_id) as num_learners_from_group, subject_id
from your_table group by subject_id
having num_learners_from_group =
(select count(*)
from stud_groups_table
where stud_groups_table.stud_group_id = your_table.stud_group_id
group by stud_groups_table.stud_group_id)
您尚未提及表名,因此使用TABLE_NAME作为占位符。
select distinct subject_id from TABLE_NAME group by stud_group_id, subject_id
Hope this works for you: 希望这对您有用:
select subject_id
from mytable
group by subject_id
having count(*) = (select count(distinct stud_group_id) from mytable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.