mysql查询练习1篇--简单查询数据

mysql查询练习1篇

学生表

学号
姓名
性别
出生日期
所在班级

create table student( 
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(10) not null, 
sbirthday datetime,
class varchar(20)
)
ENGINE=innoDB
DEFAULT CHARACTER SET=utf8
;

教师表

教师编号
教师名字
教师性别
出生日期
职称
所在部门

create table teacher( 
tno varchar(20) primary key,
tname varchar(20) not null,
tsex varchar(10) not null,
tbirthday datetime, 
prof varchar(20) not null,
depart varchar(20) not null
)
ENGINE=innoDB
DEFAULT CHARACTER SET=utf8
;

课程表

课程号
课程名称
教师编号

create table course(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(10) not null,
foreign key(tno) references teacher(tno)
)
ENGINE=innoDB
DEFAULT CHARACTER SET=utf8
;

成绩表

学号
课程号
成绩

create table score( 
sno varchar(20) not null,
cno varchar(20) not null,
degree decimal, 
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
primary key(sno,cno)
)
ENGINE=innoDB
DEFAULT CHARACTER SET=utf8;

往数据表中添加数据

--学生表数据
INSERT INTO student VALUES('101','曾华','男','1977-09-01','95033');
INSERT INTO student VALUES('102','匡明','男','1975-10-02','95031');
INSERT INTO student VALUES('103','王丽','女','1976-01-23','95033');
INSERT INTO student VALUES('104','李军','男','1976-02-20','95033');
INSERT INTO student VALUES('105','王芳','女','1975-02-10','95031');
INSERT INTO student VALUES('106','陆军','男','1974-06-03','95031');
INSERT INTO student VALUES('107','王二','男','1976-02-20','95033');
INSERT INTO student VALUES('108','张全蛋','男','1975-02-10','95031');
INSERT INTO student VALUES('109','赵铁柱','男','1974-06-03','95031');

--教师表数据
INSERT INTO teacher VALUES('804','李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO teacher VALUES('856','张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO teacher VALUES('825','王萍','女','1972-05-05','助教','计算机系');
INSERT INTO teacher VALUES('831','刘冰','女','1977-08-14','助教','电子工程系');

--添加课程表
INSERT INTO course VALUES('3-105','计算机导论','825');
INSERT INTO course VALUES('3-245','操作系统','804');
INSERT INTO course VALUES('6-166','数字电路','856');
INSERT INTO course VALUES('9-888','高等数学','831');

--添加成绩表
INSERT INTO score VALUES('103','3-245','86');
INSERT INTO score VALUES('105','3-245','75');
INSERT INTO score VALUES('109','3-245','68');
INSERT INTO score VALUES('103','3-105','92');
INSERT INTO score VALUES('105','3-105','88');
INSERT INTO score VALUES('109','3-105','76');
INSERT INTO score VALUES('103','6-166','85');
INSERT INTO score VALUES('105','6-166','79');
INSERT INTO score VALUES('109','6-166','81');

查询练习

1.查询student表中所有(*)记录

mysql> select * from student;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾华      || 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明      || 1975-10-02 00:00:00 | 95031 |
| 103 | 王丽      || 1976-01-23 00:00:00 | 95033 |
| 104 | 李军      || 1976-02-20 00:00:00 | 95033 |
| 105 | 王芳      || 1975-02-10 00:00:00 | 95031 |
| 106 | 陆军      || 1974-06-03 00:00:00 | 95031 |
| 107 | 王二      || 1976-02-20 00:00:00 | 95033 |
| 108 | 张全蛋    || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    || 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec)

2.查询student表中所有记录的sname,ssex和class列

mysql> select sname,ssex,class from student;
+-----------+------+-------+
| sname     | ssex | class |
+-----------+------+-------+
| 曾华      || 95033 |
| 匡明      || 95031 |
| 王丽      || 95033 |
| 李军      || 95033 |
| 王芳      || 95031 |
| 陆军      || 95031 |
| 王二      || 95033 |
| 张全蛋    || 95031 |
| 赵铁柱    || 95031 |
+-----------+------+-------+

3.查询教师所有的单位但是不重复的depart列 (distinct)

mysql> select depart from teacher;
+-----------------+
| depart          |
+-----------------+
| 计算机系        |
| 计算机系        |
| 电子工程系      |
| 电子工程系      |
+-----------------+
4 rows in set (0.00 sec)
//distinct排除重复
mysql> select distinct depart from teacher;
+-----------------+
| depart          |
+-----------------+
| 计算机系        |
| 电子工程系      |
+-----------------+
2 rows in set (0.00 sec)

4.查询score表中成绩(degree)在60-80之间所有的记录
查询区间:

mysql> select * from score where degree between 60 and 80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
4 rows in set (0.00 sec)
//方法2,直接用运算符比较
mysql> select * from score where degree > 60 and degree < 80;

5.查询score表中成绩为85, 86, 或者88的记录(或者关系,in

mysql> select * from score where degree in(85,86,88);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
+-----+-------+--------+
3 rows in set (0.00 sec)

6.查询student表中’95031’班或者性别为’女’的同学记录
不同字段间的或者,用 or

mysql> select * from student where class='95031' or ssex='女';
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 102 | 匡明      || 1975-10-02 00:00:00 | 95031 |
| 103 | 王丽      || 1976-01-23 00:00:00 | 95033 |
| 105 | 王芳      || 1975-02-10 00:00:00 | 95031 |
| 106 | 陆军      || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋    || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    || 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+

7.以class降序查询student表中所有的记录

//降序  desc
mysql> select * from student order by class desc;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾华      || 1977-09-01 00:00:00 | 95033 |
| 103 | 王丽      || 1976-01-23 00:00:00 | 95033 |
| 104 | 李军      || 1976-02-20 00:00:00 | 95033 |
| 107 | 王二      || 1976-02-20 00:00:00 | 95033 |
| 102 | 匡明      || 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳      || 1975-02-10 00:00:00 | 95031 |
| 106 | 陆军      || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋    || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    || 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec)
//默认升序,什么都不加 或者加asc
mysql> select * from student order by class ;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 102 | 匡明      || 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳      || 1975-02-10 00:00:00 | 95031 |
| 106 | 陆军      || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋    || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    || 1974-06-03 00:00:00 | 95031 |
| 101 | 曾华      || 1977-09-01 00:00:00 | 95033 |
| 103 | 王丽      || 1976-01-23 00:00:00 | 95033 |
| 104 | 李军      || 1976-02-20 00:00:00 | 95033 |
| 107 | 王二      || 1976-02-20 00:00:00 | 95033 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec

8.以cno升序、degree降序查询score表中所有的数据
先以cno升序排序,相同时再按照degree降序排序。

mysql> select * from score order by cno asc,degree desc ;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 105 | 3-105 |     88 |
| 109 | 3-105 |     76 |
| 103 | 3-245 |     86 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
| 103 | 6-166 |     85 |
| 109 | 6-166 |     81 |
| 105 | 6-166 |     79 |
+-----+-------+--------+
9 rows in set (0.00 sec)

9.查询’95031’班的学生人数。(统计,count)

mysql> select count(*) from student where class='95031';
+----------+
| count(*) |
+----------+
|        5 |
+----------+

10.查询score表中的最高分数的学生号和课程号.(子查询)
(1)找到最高分select max(degree) from score;
(2)找最高分的sno和cno select sno,cno from score where degree…

mysql> select sno,cno from score where degree=(select max(degree) from score);
+-----+-------+
| sno | cno   |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)

方法2
排序法

mysql> select sno,cno,degree from score order by degree;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 109 | 3-245 |     68 |
| 105 | 3-245 |     75 |
| 109 | 3-105 |     76 |
| 105 | 6-166 |     79 |
| 109 | 6-166 |     81 |
| 103 | 6-166 |     85 |
| 103 | 3-245 |     86 |
| 105 | 3-105 |     88 |
| 103 | 3-105 |     92 |
+-----+-------+--------+

mysql> select sno,cno,degree from score order by degree desc limit 0,1;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
+-----+-------+--------+
1 row in set (0.00 sec)

//limit 第一个数字表示从哪开始第二个数字表示查多少条
mysql> select sno,cno,degree from score order by degree desc limit 1,2;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-105 |     88 |
| 103 | 3-245 |     86 |
+-----+-------+--------+
2 rows in set (0.00 sec)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值