(-1)写在前面
文章参考http://blog.sina.com.cn/willcaty。
针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答。
(0) 基础数据
student表
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大|男| 1985 |计算机系|北京市海淀区|
| 904 | 李四|男| 1990 |英语系|辽宁省阜新市|
| 905 | 王五|女| 1991 |英语系|福建省厦门市|
| 906 | 王六|男| 1988 |计算机系|湖南省衡阳市|
+-----+--------+------+-------+------------+--------------+
score表
+----+--------+-----------+-------+
| Id | Stu_id | C_Name | Grade |
+----+--------+-----------+-------+
| 23 | 901 | 计算机| 98 |
| 24 | 901 | 英语| 80 |
| 25 | 902 | 计算机| 65 |
| 26 | 902 | 中文| 88 |
| 27 | 903 | 中文| 95 |
| 28 | 904 | 计算机| 70 |
| 29 | 904 | 英语| 92 |
| 30 | 905 | 英语| 94 |
| 31 | 906 | 计算机| 90 |
| 32 | 906 | 英语| 85 |
+----+--------+-----------+-------+
(1)查询同时参加计算机和英语考试的学生的信息
方式一:
SELECT a.* FROM student a ,score b ,score c
WHERE a.id=b.stu_id
AND b.c_name=‘计算机‘
AND a.id=c.stu_id
AND c.c_name=‘英语‘;
方式二:
SELECT * FROM student
WHERE id =ANY
( SELECT stu_id FROM score
WHERE stu_id IN (
SELECT stu_id FROM
score WHERE c_name= ‘计算机‘)
AND c_name= ‘英语‘ );
方式三:
select * from student where id in(
select s.stu_id from (select stu_id from score where c_name = ‘计算机‘) s
(select stu_id from score where c_name=‘英语‘) as t where s.stu_id=t.stu_id)
方式四:
select * from student where id in (
select stu_id from score where c_name =‘计算机‘ and stu_id in(
select stu_id from score where c_name =‘计算机‘));
(2) 正确答案
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+