这里有张桌子:

+-------------+----------+
|stud_group_id|subject_id|
+-------------+----------+
|    1g       |   1s     |
+-------------+----------+
|    1g       |   2s     |
+-------------+----------+
|    1g       |   3s     |
+-------------+----------+
|    2g       |   1s     |
+-------------+----------+
|    2g       |   2s     |
+-------------+----------+
|    3g       |   1s     |
+-------------+----------+
|    3g       |   2s     |
+-------------+----------+
|    3g       |   4s     |
+-------------+----------+


我需要选择所有subject_id学到的stud_group_id
我预计

+----------+
|subject_id|
+----------+
|   1s     |
+----------+
|   2s     |
+----------+


我怎样才能做到这一点?谢谢

最佳答案

试试这个:

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>)

关于mysql - 如何选择所有行都具有的行ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57852031/

10-12 02:40