sql 如何表达有且仅有,例如科目最多选择10项,查询有且仅有选择指定2项的学员。
In 语句可以进行限制。个人认为,这种比较业务的行为最好放在上层的 Service 去进行控制。
先选择选择了指定科目的人,然后 not exists 其他科目
select * from 选择科目表 a where 科目名称='指定名称' and not exists(select 1 from 选择科目表 where 学生id=a.学生id and 科目名称<>'指定名称')