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

mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风的更多相关文章
- MySQL中查询获取每个班级成绩前三名的学生信息
CREATE TABLE t_testscore( pk_id INT PRIMARY KEY, c_name VARCHAR(50) , c_score INT, c_class INT )DEFA ...
- MySQL练习题1
以下SQL操作均在MYSQL上测试过 首先是表定义 1.创建student和score表 CREATE TABLE student ( id ) NOT NULL UNIQUE PRIMARY KEY ...
- mysql经典查询语句-笔记
笔记来源公开课,谢谢! 1.创建student和score表 CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name ...
- 笔记-4:mysql数据查询
1.创建查询表 1.1 创建班级表 含义 字段名 数据类型 宽度 班级编号 classNo 字符型 6 班级名称 className 字符型 20 所属院系 department 字符型 30 年级 ...
- mysql 分组查询前n条数据
今天去面试,碰到一道面试题: 有一个学生成绩表,表中有 表id.学生名.学科.分数.学生id .查询每科学习最好的两名学生的信息: 建表sql: CREATE TABLE `stuscore` ( ` ...
- MYSQL之查询篇
2. 数据库操作 数据库在创建以后最常见的操作便是查询 2.1 查询 为了便于学习和理解,我们预先准备了两个表分别是stduents表和classes表两个表的内容和结构如下所示 students表的 ...
- MySQL 表查询语句练习题
MySQL 表查询语句练习题: 一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表 ...
- MYSQL select查询练习题
10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree=(select max(degree) from ...
- mysql常见查询练习题
#建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...
随机推荐
- canvas贝塞尔曲线 - 1
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvEAAAIcCAIAAADwgGbqAAAgAElEQVR4nOy9a4wb55nn20kOZhq7o8
- grep 命令过滤配置文件中的注释和空行
grep 用法 Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. ...
- html tab页面切换事件。
$(document).bind("visibilitychange",function(e){ //只对tab页面切换有效 //document.visibilityState ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 通过源码了解ASP.NET MVC 几种Filter的执行过程
一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神的工作,而且很多人觉得平时根本不需要知道这些,会用就行了.其实阅读源 ...
- css
1.css代码语法 p{font-size:12px;color:red;}p 为选择符:又称选择器,指明网页中要应用样式规则的元素,如本例中是网页中所有的段(p)的文字将变成蓝色,而其他的元素(如o ...
- keras 中如何自定义损失函数
http://lazycoderx.com/2016/10/09/keras%E4%BF%9D%E5%AD%98%E6%A8%A1%E5%9E%8B%E6%97%B6%E4%BD%BF%E7%94%A ...
- Android -- 获取接口数据的三个方法
1. compile 'com.loopj.android:android-async-http:1.4.9': AsyncHttpClient client = new AsyncHttpCli ...
- .Net4.0以上使用System.Data.Sqlite
最近对Sqlite感兴趣,就尝试了一下用c#连接,我用的版本是vs2013,默认开发环境是.net4.5,,按照网上的教材,下载了System.Data.Sqlite,然后写了下面这个简单的测试代码, ...