(-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练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风的更多相关文章

  1. MySQL中查询获取每个班级成绩前三名的学生信息

    CREATE TABLE t_testscore( pk_id INT PRIMARY KEY, c_name VARCHAR(50) , c_score INT, c_class INT )DEFA ...

  2. MySQL练习题1

    以下SQL操作均在MYSQL上测试过 首先是表定义 1.创建student和score表 CREATE TABLE student ( id ) NOT NULL UNIQUE PRIMARY KEY ...

  3. mysql经典查询语句-笔记

    笔记来源公开课,谢谢! 1.创建student和score表 CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name ...

  4. 笔记-4:mysql数据查询

    1.创建查询表 1.1 创建班级表 含义 字段名 数据类型 宽度 班级编号 classNo 字符型 6 班级名称 className 字符型 20 所属院系 department 字符型 30 年级 ...

  5. mysql 分组查询前n条数据

    今天去面试,碰到一道面试题: 有一个学生成绩表,表中有 表id.学生名.学科.分数.学生id .查询每科学习最好的两名学生的信息: 建表sql: CREATE TABLE `stuscore` ( ` ...

  6. MYSQL之查询篇

    2. 数据库操作 数据库在创建以后最常见的操作便是查询 2.1 查询 为了便于学习和理解,我们预先准备了两个表分别是stduents表和classes表两个表的内容和结构如下所示 students表的 ...

  7. MySQL 表查询语句练习题

    MySQL 表查询语句练习题: 一.  设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表 ...

  8. MYSQL select查询练习题

    10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree=(select max(degree) from ...

  9. mysql常见查询练习题

    #建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...

随机推荐

  1. HTML5 & 三年二班周杰伦

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Linux下的ctrl常用组合键

    在linux的命令模式下使用ctrl组合键能让操作更便捷. ctrl + k -- 剪切光标及其后边的内容: ctrl + u -- 剪切光标之前的内容: ctrl + y -- 在光标处粘贴上两个命 ...

  3. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  4. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  5. C++:通过gethostbyname函数,根据服务器的域名,获取服务器IP

    本代码的编译环境为MAC,系统版本为10.11.6: #include <string.h> #include <netdb.h> #include <stdio.h&g ...

  6. Markdown语法 中文版

    文章翻译自Markdown创始人JOHN GRUBER的 个人博客, 英文原文请参见 Markdown Syntax; 本文地址: http://www.cnblogs.com/ayning/p/43 ...

  7. vcf格式

    Variant Call Format(VCF)是一个用于存储基因序列突变信息的文本格式.表示单碱基突变, 插入/缺失, 拷贝数变异和结构变异等.BCF格式文件是VCF格式的二进制文件. CHROM ...

  8. Android 天猫apk聊天数据库解密

    1.使用Android 天猫apk 进行聊天会产生tmallWangXinDB的数据库.2.用sqlite3 工具打开提示加密或者错误.3.需要对该数据库进行解密. 解密流程:1.反编译apk,dex ...

  9. 使用Xmanager访问CentOS远程桌面

    最近在搞Qemu虚拟机相关的项目,需要用到Linux的桌面系统,用Xmanager连接CentOS桌面最方便了. Linux端:CentOS release 6.8 (Final) Windows端: ...

  10. 8 HTML&JS等前端知识系列之jquery的自定义方法

    preface 有时候我们在前端写jquery的时候,会自己自定义些方法,这样可以不需要重复造轮子.先说说2种自定义方法的区别: 不跟在选择器后面的 跟在选择器后面的. 那下面说说如何自定义jquer ...