MySQL查询的一个例题的多种解法

(-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 | 计算机系 | 湖南省衡阳市 |

±----±-------±-----±------±-----------±-------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值