您好我有一个名为student_subject_table的mysql表:
+----+-------------+---------------------+
| id | student_id | subject |
+----+-------------+---------------------+
| 1 | 7 | math |
| 2 | 7 | physics |
| 3 | 7 | chemistry |
| 8 | 8 | math |
| 9 | 8 | physics |
| 10 | 8 | chemistry |
| 11 | 8 | biology |
+----+-------------+---------------------+
我试图让那些只有数学,物理和化学成分的学生ids。
我在条款中试过:
select student_id
from student_subject table
where
subject in ('math', 'physics', 'chemistry')
GROUP BY (student_id)
正如预期的那样,我将7和8作为student_id。 在上表中我只需要student_id = 7.
在mysql中我有什么办法吗?
请帮忙。
答案 0 :(得分:2)
您可以使用此查询:
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // you USUALLY want this
// likely you should limit this to specific origins
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
source.registerCorsConfiguration("/logout", config);
return new CorsFilter(source);
}
select student_id
from student_subject table
group by student_id
having
SUM(subject IN ('math', 'physics', 'chemistry')) = COUNT(DISTINCT subject)
AND COUNT(DISTINCT subject)=3
将为3 SUM(subject IN ('math', 'physics', 'chemistry'))
将为学生7为3,为学生8为如果COUNT(DISTINCT subject)
是唯一的,您可以将(student_id, subject)
替换为COUNT(DISTINCT ...)
。
只返回学生7。
答案 1 :(得分:0)
使用DISTINCT条款
SELECT DISTINCT student_id
FROM student_subject_table
WHERE subject IN ('math', 'physics', 'chemistry')
GROUP BY (student_id);