mysql 联合查询

MySql 码拜 7年前 (2017-05-06) 1571次浏览
mysql 联合查询
mysql 联合查询
—  查询同时参加计算机和英语考试的学生的信息
问一下该怎么查询?
解决方案

10

SELECT  a.* FROM    student a JOIN ( SELECT  Stu_id ,COUNT(1) AS num FROM score WHERE C_name IN ( “计算机”, “英语” ) GROUP BY Stu_id HAVING num >1 ) t ON a.id = t.Stu_idmysql 联合查询
你再本地试下

30

where引入计算机和英语,having引入count>1就可以了:
select student.*,score.stu_id
from student left join score
on student.id=score.stu_id
where score.c_name in (“计算机”,”英语”)
group by student.id
having count(student.id)>1
;
+–+–+–+–+–+–+–+
| id  | NAME   | sex  | birth | department | address      | stu_id |
+–+–+–+–+–+–+–+
| 901 | 令狐冲 | 男   |  1995 | 计算机系   | 北京市海淀区 |    901 |
| 904 | 任本人行 | 男   |  1990 | 英语系     | 辽宁省阜新市 |    904 |
| 906 | 向问天 | 男   |  1998 | 计算机系   | 广州省深圳市 |    906 |
| 907 | 周芷若 | 女   |  1999 | 计算机系   | 湖北省武汉市 |    907 |
+–+–+–+–+–+–+–+
4 rows in set (0.00 sec)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明mysql 联合查询
喜欢 (0)
[1034331897@qq.com]
分享 (0)