MySQL經(jīng)典練習(xí)題&面試題(持續(xù)更新……)

最近在學(xué)MySQL,網(wǎng)上有很多版本的MySQL經(jīng)典50題解答,有時間會二刷并且加入性能方面的考量,后續(xù)也會繼續(xù)整理加入收集到的面試題目。

原始表格及題目來源網(wǎng)絡(luò),分為四張表

1.學(xué)生表
Student(s_id,s_name,s_birth,s_sex)
學(xué)生編號,學(xué)生姓名, 出生年月,學(xué)生性別
2.課程表
Course(c_id,c_name,t_id)
課程編號, 課程名稱, 教師編號
3.教師表
Teacher(t_id,t_name)
教師編號,教師姓名
4.成績表
Score(s_id,c_id,s_score)
學(xué)生編號,課程編號,分?jǐn)?shù)

------------------------------創(chuàng)建數(shù)據(jù)庫、表、插入數(shù)據(jù)------------------------------

#創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE test;

#選擇數(shù)據(jù)庫
USE test;

#建表
#學(xué)生表
CREATE TABLE Student(
   s_id VARCHAR(20) COMMENT '學(xué)生編號',
   s_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '學(xué)生姓名',
   s_birth VARCHAR(20) NOT NULL DEFAULT '' COMMENT '出生年月',
   s_sex VARCHAR(10) NOT NULL DEFAULT '' COMMENT '學(xué)生性別',
   PRIMARY KEY(s_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '學(xué)生表';

#課程表
CREATE TABLE Course(
   c_id  VARCHAR(20) COMMENT '課程編號',
   c_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '課程名稱',
   t_id VARCHAR(20) NOT NULL COMMENT '教師編號',
   PRIMARY KEY(c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '課程表';

#教師表
CREATE TABLE Teacher(
   t_id VARCHAR(20) COMMENT '教師編號',
   t_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '教師姓名',
   PRIMARY KEY(t_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '教師表';

#成績表
CREATE TABLE Score(
   s_id VARCHAR(20) COMMENT '學(xué)生編號',
   c_id  VARCHAR(20) COMMENT '課程編號',
   s_score INT(3) COMMENT '分?jǐn)?shù)',
   PRIMARY KEY(s_id,c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '成績表';

#插入學(xué)生表測試數(shù)據(jù)
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風(fēng)' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-03-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-07-01' , '女');
insert into Student values('09' , '張三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '趙六' , '2017-01-01' , '女');
insert into Student values('13' , '孫七' , '2018-01-01' , '女');


#課程表測試數(shù)據(jù)
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數(shù)學(xué)' , '01');
insert into Course values('03' , '英語' , '03');

#教師表測試數(shù)據(jù)
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

#成績表測試數(shù)據(jù)
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

-----------------------------------------習(xí)題解答-----------------------------------------

1、查詢"01"課程比"02"課程成績高的學(xué)生的信息及課程分?jǐn)?shù)

select s.*,sc.s_score AS c1,sc2.s_score AS c2 from score sc left join student s on sc.s_id=s.s_id inner join score sc2 on sc.s_id=sc2.s_id and sc.c_id='01' and sc2.c_id='02' where sc.s_score>sc2.s_score;

正確輸出:
02  錢電  1990-12-21  男   70  60
04  李云  1990-08-06  男   50  30

1.1、查詢同時存在" 01 "課程和" 02 "課程的情況

A.
select a.*,b.c_id,b.s_score 
  from score as a left join score as b 
  on a.s_id=b.s_id 
  where a.c_id='01' and b.c_id='02';
B.
select a.*,b.c_id,b.s_score
  from score as a inner join score as b 
  on a.s_id=b.s_id and a.c_id='01' and b.c_id='02';

#left join后一定要接where
#因為and會合并滿足a.s_id=b.s_id and a.c_id='01' and b.c_id='02'的位置
#但最后會以left表為基準(zhǔn),輸出null值記錄

正確輸出:
+------+------+---------+------+---------+
| s_id | c_id | s_score | c_id | s_score |
+------+------+---------+------+---------+
| 01   | 01   |      80 | 02   |      90 |
| 02   | 01   |      70 | 02   |      60 |
| 03   | 01   |      80 | 02   |      80 |
| 04   | 01   |      50 | 02   |      30 |
| 05   | 01   |      76 | 02   |      87 |
+------+------+---------+------+---------+

1.2、查詢存在" 01 "課程但可能不存在" 02 "課程的情況(不存在時顯示為 null)

select a.*,b.c_id,b.s_score 
  from score as a left join score as b 
  on a.s_id=b.s_id and b.c_id='02' 
  where a.c_id='01';

正確輸出:
+------+------+---------+------+---------+
| s_id | c_id | s_score | c_id | s_score |
+------+------+---------+------+---------+
| 01   | 01   |      80 | 02   |      90 |
| 02   | 01   |      70 | 02   |      60 |
| 03   | 01   |      80 | 02   |      80 |
| 04   | 01   |      50 | 02   |      30 |
| 05   | 01   |      76 | 02   |      87 |
| 06   | 01   |      31 | NULL |    NULL |
+------+------+---------+------+---------+

1.3、查詢不存在" 01 "課程但存在" 02 "課程的情況

select s_id from score where s_id not in 
  (select s_id from score where c_id='01')
  and c_id='02';

正確輸出:
+------+
| s_id |
+------+
| 07   |
+------+

2、查詢平均成績大于等于60分的同學(xué)的學(xué)生編號和學(xué)生姓名和平均成績

select s.*,round(avg(s_score),2) avg from student s inner join score sc on s.s_id=sc.s_id group by s_id having avg>=60;

正確輸出:
+------+--------+------------+-------+-------+
| s_id | s_name | s_birth    | s_sex | avg   |
+------+--------+------------+-------+-------+
| 01   | 趙雷   | 1990-01-01 | 男    | 89.67 |
| 02   | 錢電   | 1990-12-21 | 男    | 70.00 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    | 80.00 |
| 05   | 周梅   | 1991-12-01 | 女    | 81.50 |
| 07   | 鄭竹   | 1989-07-01 | 女    | 93.50 |
+------+--------+------------+-------+-------+

☆3、查詢在 SC 表存在成績的學(xué)生信息

Q:
①怎么看哪種方法更好?
set profiling=1;show profiles;結(jié)果好像有波動,數(shù)據(jù)量小的時候看不出哪種方法更快
查了一些explain結(jié)果的含義,但不知道是不是具體效率和extra里的內(nèi)容也有關(guān)系。
例如這題,按理說join更快,但A.B兩種方法的type,key,row都相同,explain只有extra細(xì)微區(qū)別,
實際show profiles看執(zhí)行速度的時候似乎有波動,不能確定哪個更快。
②用row_number()去重是不是只能多加一層嵌套,如C,是不是沒有g(shù)roup高效?

A.select * from student where s_id in (select s_id from score group by s_id);

B.select s.* from student s inner join score sc on s.s_id=sc.s_id group by s.s_id;

C.select t.s_id,t.s_name,t.s_birth,t.s_sex from (select s.*,row_number() over (partition by s_id) as rank1 from student s inner join score sc on s.s_id=sc.s_id) t where rank1=1 ;

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 01   | 趙雷   | 1990-01-01 | 男    |
| 02   | 錢電   | 1990-12-21 | 男    |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |
| 04   | 李云   | 1990-08-06 | 男    |
| 05   | 周梅   | 1991-12-01 | 女    |
| 06   | 吳蘭   | 1992-03-01 | 女    |
| 07   | 鄭竹   | 1989-07-01 | 女    |
+------+--------+------------+-------+

A.explain select * from student where s_id in (select s_id from score group by s_id);
+----+-------------+---------+------------+--------+---------------+---------+---------+-----------------+------+----------+------------------------+
| id | select_type | table   | partitions | type   | possible_keys | key     | key_len | ref             | rows | filtered | Extra                  |
+----+-------------+---------+------------+--------+---------------+---------+---------+-----------------+------+----------+------------------------+
|  1 | SIMPLE      | score   | NULL       | index  | PRIMARY       | PRIMARY | 124     | NULL            |   18 |    38.89 | Using index; LooseScan |
|  1 | SIMPLE      | student | NULL       | eq_ref | PRIMARY       | PRIMARY | 62      | test.score.s_id |    1 |   100.00 | NULL                   |
+----+-------------+---------+------------+--------+---------------+---------+---------+-----------------+------+----------+------------------------+
B.explain select s.* from student s inner join score sc on s.s_id=sc.s_id group by s.s_id;
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+------------------------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref          | rows | filtered | Extra                        |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+------------------------------+
|  1 | SIMPLE      | sc    | NULL       | index  | PRIMARY       | PRIMARY | 124     | NULL         |   18 |   100.00 | Using index; Using temporary |
|  1 | SIMPLE      | s     | NULL       | eq_ref | PRIMARY       | PRIMARY | 62      | test.sc.s_id |    1 |   100.00 | NULL                         |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+------------------------------+
C.explain select t.s_id,t.s_name,t.s_birth,t.s_sex from (select s.*,row_number() over (partition by s_id) as rank1 from student s inner join score sc on s.s_id=sc.s_id) t where rank1=1 ;
+----+-------------+------------+------------+--------+---------------+-------------+---------+--------------+------+----------+----------------------------------------------+
| id | select_type | table      | partitions | type   | possible_keys | key         | key_len | ref          | rows | filtered | Extra                                        |
+----+-------------+------------+------------+--------+---------------+-------------+---------+--------------+------+----------+----------------------------------------------+
|  1 | PRIMARY     | <derived2> | NULL       | ref    | <auto_key0>   | <auto_key0> | 8       | const        |    1 |   100.00 | NULL                                         |
|  2 | DERIVED     | sc         | NULL       | index  | PRIMARY       | PRIMARY     | 124     | NULL         |   18 |   100.00 | Using index; Using temporary; Using filesort |
|  2 | DERIVED     | s          | NULL       | eq_ref | PRIMARY       | PRIMARY     | 62      | test.sc.s_id |    1 |   100.00 | NULL                                         |
+----+-------------+------------+------------+--------+---------------+-------------+---------+--------------+------+----------+----------------------------------------------+

4、查詢所有同學(xué)的學(xué)生編號、學(xué)生姓名、選課總數(shù)、所有課程的總成績

select s.*,(case when s_score is not null then count(1) else 0 end) as num,sum(case when s_score is not null then s_score else 0 end) as total from student s left join score sc on s.s_id=sc.s_id group by s.s_id;

正確輸出:
+------+--------+------------+-------+-----+-------+
| s_id | s_name | s_birth    | s_sex | num | total |
+------+--------+------------+-------+-----+-------+
| 01   | 趙雷   | 1990-01-01 | 男    |   3 |   269 |
| 02   | 錢電   | 1990-12-21 | 男    |   3 |   210 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |   3 |   240 |
| 04   | 李云   | 1990-08-06 | 男    |   3 |   100 |
| 05   | 周梅   | 1991-12-01 | 女    |   2 |   163 |
| 06   | 吳蘭   | 1992-03-01 | 女    |   2 |    65 |
| 07   | 鄭竹   | 1989-07-01 | 女    |   2 |   187 |
| 09   | 張三   | 2017-12-20 | 女    |   0 |     0 |
| 10   | 李四   | 2017-12-25 | 女    |   0 |     0 |
| 11   | 李四   | 2017-12-30 | 女    |   0 |     0 |
| 12   | 趙六   | 2017-01-01 | 女    |   0 |     0 |
| 13   | 孫七   | 2018-01-01 | 女    |   0 |     0 |
+------+--------+------------+-------+-----+-------+

5、查詢"李"姓老師的數(shù)量

SELECT COUNT(t_name) AS TeacherNamedLi FROM teacher WHERE t_name LIKE "李%"

正確輸出:
1

6、查詢學(xué)過"張三"老師授課的同學(xué)的信息

select s.* from student s inner join score sc on s.s_id=sc.s_id inner join course c on c.c_id=sc.c_id inner join teacher t on t.t_id=c.t_id where t.t_name="張三";

正確輸出:
01  趙雷  1990-01-01  男
02  錢電  1990-12-21  男
03  孫風(fēng)  1990-05-20  男
04  李云  1990-08-06  男
05  周梅  1991-12-01  女
07  鄭竹  1989-07-01  女

7、查詢沒有學(xué)全所有課程的同學(xué)的信息

select s.* from student s left join score sc on s.s_id=sc.s_id group by s.s_id having count(1)<3;

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 05   | 周梅   | 1991-12-01 | 女    |
| 06   | 吳蘭   | 1992-03-01 | 女    |
| 07   | 鄭竹   | 1989-07-01 | 女    |
| 09   | 張三   | 2017-12-20 | 女    |
| 10   | 李四   | 2017-12-25 | 女    |
| 11   | 李四   | 2017-12-30 | 女    |
| 12   | 趙六   | 2017-01-01 | 女    |
| 13   | 孫七   | 2018-01-01 | 女    |
+------+--------+------------+-------+

☆8、查詢至少有一門課與學(xué)號為" 01 "的同學(xué)所學(xué)相同的同學(xué)的信息

Q:這題和3有些像,還是不清楚哪種情況更快。
老師之前有在群里說過distinct和子查詢最好不要用
但看explain結(jié)果type,ref、row都是子查詢方法更佳。

A.用join
select s.* from student s left join score sc on s.s_id=sc.s_id where c_id in (select c_id from score where s_id='01') group by s_id;
explain select s.* from student s left join score sc on s.s_id=sc.s_id where c_id in (select c_id from score where s_id='01') group by s_id;
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-----------------------------------------------------------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref          | rows | filtered | Extra                                                           |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-----------------------------------------------------------------+
|  1 | SIMPLE      | score | NULL       | ref    | PRIMARY       | PRIMARY | 62      | const        |    3 |   100.00 | Using index; Using temporary                                    |
|  1 | SIMPLE      | sc    | NULL       | index  | PRIMARY       | PRIMARY | 124     | NULL         |   18 |    10.00 | Using where; Using index; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | s     | NULL       | eq_ref | PRIMARY       | PRIMARY | 62      | test.sc.s_id |    1 |   100.00 | NULL                                                            |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-----------------------------------------------------------------+

B.子查詢group
select * from student where s_id in (select distinct s_id from score where c_id in (select c_id from score where s_id='01') and s_id!='01');
explain select * from student where s_id in (select distinct s_id from score where c_id in (select c_id from score where s_id='01') and s_id!='01');
+----+-------------+---------+------------+--------+---------------+---------+---------+-----------------------+------+----------+-------------------------------------+
| id | select_type | table   | partitions | type   | possible_keys | key     | key_len | ref                   | rows | filtered | Extra                               |
+----+-------------+---------+------------+--------+---------------+---------+---------+-----------------------+------+----------+-------------------------------------+
|  1 | SIMPLE      | score   | NULL       | range  | PRIMARY       | PRIMARY | 62      | NULL                  |   16 |    43.75 | Using where; Using index; LooseScan |
|  1 | SIMPLE      | score   | NULL       | eq_ref | PRIMARY       | PRIMARY | 124     | const,test.score.c_id |    1 |   100.00 | Using index; FirstMatch(score)      |
|  1 | SIMPLE      | student | NULL       | eq_ref | PRIMARY       | PRIMARY | 62      | test.score.s_id       |    1 |   100.00 | NULL                                |
+----+-------------+---------+------------+--------+---------------+---------+---------+-----------------------+------+----------+-------------------------------------+

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 02   | 錢電   | 1990-12-21 | 男    |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |
| 04   | 李云   | 1990-08-06 | 男    |
| 05   | 周梅   | 1991-12-01 | 女    |
| 06   | 吳蘭   | 1992-03-01 | 女    |
| 07   | 鄭竹   | 1989-07-01 | 女    |
+------+--------+------------+-------+

☆9、查詢和"01"號的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)的信息

Q:想問下老師還有沒有更好的方法?

select @group:=group_concat(c_id) 
  from score group by s_id having s_id='01';

select s.* from student s inner join score sc on sc.s_id=s.s_id 
  where s.s_id!='01' group by s.s_id having group_concat(c_id)=@group;

正確輸出
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 02   | 錢電   | 1990-12-21 | 男    |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |
| 04   | 李云   | 1990-08-06 | 男    |
+------+--------+------------+-------+

10、查詢沒學(xué)過"張三"老師授課的同學(xué)的信息

select s.* from student s inner join score sc on s.s_id=sc.s_id inner join course c on c.c_id=sc.c_id inner join teacher t on t.t_id=c.t_id where t.t_name!="張三";

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 06   | 吳蘭   | 1992-03-01 | 女    |
| 09   | 張三   | 2017-12-20 | 女    |
| 10   | 李四   | 2017-12-25 | 女    |
| 11   | 李四   | 2017-12-30 | 女    |
| 12   | 趙六   | 2017-01-01 | 女    |
| 13   | 孫七   | 2018-01-01 | 女    |
+------+--------+------------+-------+

11、查詢兩門及其以上不及格課程的同學(xué)的學(xué)號,姓名及其平均成績

select s.s_id,s.s_name,round(avg(s_score),2) as avg from student s inner join score sc on s.s_id=sc.s_id where s.s_id in(select s_id from score where s.s_score<60 group by s_id having count(1)>=2) group by s_id;

正確輸出:
+------+-------+--------+
| s_id | s_avg | s_name |
+------+-------+--------+
| 04   | 33.33 | 李云   |
| 06   | 32.50 | 吳蘭   |
+------+-------+--------+

12、檢索" 01 "課程分?jǐn)?shù)小于 60,按分?jǐn)?shù)降序排列的學(xué)生信息

select s.*,sc.s_score from student as s
  inner join score as sc
  on s.s_id=sc.s_id and sc.c_id='01' and sc.s_score<60 
  order by s_score desc;

正確輸出:
+------+--------+------------+-------+---------+
| s_id | s_name | s_birth    | s_sex | s_score |
+------+--------+------------+-------+---------+
| 04   | 李云   | 1990-08-06 | 男    |      50 |
| 06   | 吳蘭   | 1992-03-01 | 女    |      31 |
+------+--------+------------+-------+---------+

☆13、按平均成績從高到低顯示所有學(xué)生的所有課程的成績以及平均成績

Q:看到老師在群里說數(shù)據(jù)量大的時候不要出現(xiàn)太多join,但是又說應(yīng)該減少子查詢
所以我不太清楚究竟什么時候用join什么時候子查詢?

select t3.*,round(s_avg,2) as s_avg from 
  (select t2.*,s3.s_score as 03_score from
    (select t1.*,s2.s_score as 02_score from 
      (select s.*,s1.s_score as 01_score from 
        student as s left join score as s1 on s.s_id=s1.s_id and s1.c_id='01') as t1
      left join score as s2 on t1.s_id=s2.s_id and s2.c_id='02') as t2
    left join score as s3 on t2.s_id=s3.s_id and s3.c_id='03') as t3
  left join (select s_id,avg(s_score)as s_avg from score group by s_id) as t4 
on t4.s_id=t3.s_id order by s_avg desc;

正確輸出:
+------+--------+------------+-------+----------+----------+----------+-------+
| s_id | s_name | s_birth    | s_sex | 01_score | 02_score | 03_score | s_avg |
+------+--------+------------+-------+----------+----------+----------+-------+
| 07   | 鄭竹   | 1989-07-01 | 女    |     NULL |       89 |       98 | 93.50 |
| 01   | 趙雷   | 1990-01-01 | 男    |       80 |       90 |       99 | 89.67 |
| 05   | 周梅   | 1991-12-01 | 女    |       76 |       87 |     NULL | 81.50 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |       80 |       80 |       80 | 80.00 |
| 02   | 錢電   | 1990-12-21 | 男    |       70 |       60 |       80 | 70.00 |
| 04   | 李云   | 1990-08-06 | 男    |       50 |       30 |       20 | 33.33 |
| 06   | 吳蘭   | 1992-03-01 | 女    |       31 |     NULL |       34 | 32.50 |
| 11   | 李四   | 2017-12-30 | 女    |     NULL |     NULL |     NULL |  NULL |
| 12   | 趙六   | 2017-01-01 | 女    |     NULL |     NULL |     NULL |  NULL |
| 13   | 孫七   | 2018-01-01 | 女    |     NULL |     NULL |     NULL |  NULL |
| 09   | 張三   | 2017-12-20 | 女    |     NULL |     NULL |     NULL |  NULL |
| 10   | 李四   | 2017-12-25 | 女    |     NULL |     NULL |     NULL |  NULL |
+------+--------+------------+-------+----------+----------+----------+-------+

14、查詢各科成績最高分、最低分和平均分: 以如下形式顯示:課程 ID,課程 name,最高分,最低分,平均分,及格率,中等率,優(yōu)良率,優(yōu)秀率 及格為>=60,中等為:70-80,優(yōu)良為:80-90,優(yōu)秀為:>=90 要求輸出課程號和選修人數(shù),查詢結(jié)果按人數(shù)降序排列,若人數(shù)相同,按課程號升序排列

select c.c_name,t.* from 
(select c_id,
max(s_score) as max,
min(s_score) as min,
round(avg(s_score),2) as avg,
count(1) as num,
round(sum(if(s_score>=60,1,0))*100/count(1),2) as 'pass rate',
round(sum(if(s_score>=70,1,0))*100/count(1),2) as 'medium rate',
round(sum(if(s_score>=80,1,0))*100/count(1),2) as 'good rate',
round(sum(if(s_score>=90,1,0))*100/count(1),2) as 'excellent rate'
from score group by c_id) as t left join course as c on c.c_id=t.c_id 
order by num desc,c_id asc;

正確輸出:
+--------+------+------+------+-------+-----+-----------+-------------+-----------+----------------+
| c_name | c_id | max  | min  | avg   | num | pass rate | medium rate | good rate | excellent rate |
+--------+------+------+------+-------+-----+-----------+-------------+-----------+----------------+
| 語文   | 01   |   80 |   31 | 64.50 |   6 |     66.67 |       66.67 |     33.33 |           0.00 |
| 數(shù)學(xué)   | 02   |   90 |   30 | 72.67 |   6 |     83.33 |       66.67 |     66.67 |          16.67 |
| 英語   | 03   |   99 |   20 | 68.50 |   6 |     66.67 |       66.67 |     66.67 |          33.33 |
+--------+------+------+------+-------+-----+-----------+-------------+-----------+----------------+

15.按各科成績進(jìn)行排序,并顯示排名, Score 重復(fù)時保留名次空缺

select *,rank() over (partition by c_id order by s_score) as 'rank' from score;

正確輸出:
+------+------+---------+------+
| s_id | c_id | s_score | rank |
+------+------+---------+------+
| 06   | 01   |      31 |    1 |
| 04   | 01   |      50 |    2 |
| 02   | 01   |      70 |    3 |
| 05   | 01   |      76 |    4 |
| 01   | 01   |      80 |    5 |
| 03   | 01   |      80 |    5 |
| 04   | 02   |      30 |    1 |
| 02   | 02   |      60 |    2 |
| 03   | 02   |      80 |    3 |
| 05   | 02   |      87 |    4 |
| 07   | 02   |      89 |    5 |
| 01   | 02   |      90 |    6 |
| 04   | 03   |      20 |    1 |
| 06   | 03   |      34 |    2 |
| 02   | 03   |      80 |    3 |
| 03   | 03   |      80 |    3 |
| 07   | 03   |      98 |    5 |
| 01   | 03   |      99 |    6 |
+------+------+---------+------+

15.1 按各科成績進(jìn)行排序,并顯示排名, Score 重復(fù)時合并名次

select *,dense_rank() over (partition by c_id order by s_score) as 'rank' from score;

正確輸出:
+------+------+---------+------+
| s_id | c_id | s_score | rank |
+------+------+---------+------+
| 06   | 01   |      31 |    1 |
| 04   | 01   |      50 |    2 |
| 02   | 01   |      70 |    3 |
| 05   | 01   |      76 |    4 |
| 01   | 01   |      80 |    5 |
| 03   | 01   |      80 |    5 |
| 04   | 02   |      30 |    1 |
| 02   | 02   |      60 |    2 |
| 03   | 02   |      80 |    3 |
| 05   | 02   |      87 |    4 |
| 07   | 02   |      89 |    5 |
| 01   | 02   |      90 |    6 |
| 04   | 03   |      20 |    1 |
| 06   | 03   |      34 |    2 |
| 02   | 03   |      80 |    3 |
| 03   | 03   |      80 |    3 |
| 07   | 03   |      98 |    4 |
| 01   | 03   |      99 |    5 |
+------+------+---------+------+

16.查詢學(xué)生的總成績,并進(jìn)行排名,總分重復(fù)時保留名次空缺

select s.*,t.total,t.rank from(select s_id,sum(s_score) as total,rank() over (order by sum(s_score) desc)as 'rank' from score group by s_id)as t right join student as s on s.s_id=t.s_id;

正確輸出:
+------+--------+------------+-------+-------+------+
| s_id | s_name | s_birth    | s_sex | total | rank |
+------+--------+------------+-------+-------+------+
| 01   | 趙雷   | 1990-01-01 | 男    |   269 |    1 |
| 02   | 錢電   | 1990-12-21 | 男    |   210 |    3 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |   240 |    2 |
| 04   | 李云   | 1990-08-06 | 男    |   100 |    6 |
| 05   | 周梅   | 1991-12-01 | 女    |   163 |    5 |
| 06   | 吳蘭   | 1992-03-01 | 女    |    65 |    7 |
| 07   | 鄭竹   | 1989-07-01 | 女    |   187 |    4 |
| 09   | 張三   | 2017-12-20 | 女    |  NULL | NULL |
| 10   | 李四   | 2017-12-25 | 女    |  NULL | NULL |
| 11   | 李四   | 2017-12-30 | 女    |  NULL | NULL |
| 12   | 趙六   | 2017-01-01 | 女    |  NULL | NULL |
| 13   | 孫七   | 2018-01-01 | 女    |  NULL | NULL |
+------+--------+------------+-------+-------+------+

16.1 查詢學(xué)生的總成績,并進(jìn)行排名,總分重復(fù)時不保留名次空缺

select s.*,t.total,t.rank from(select s_id,sum(s_score) as total,dense_rank() over (order by sum(s_score) desc)as 'rank' from score group by s_id)as t right join student as s on s.s_id=t.s_id;

正確輸出:
+------+--------+------------+-------+-------+------+
| s_id | s_name | s_birth    | s_sex | total | rank |
+------+--------+------------+-------+-------+------+
| 01   | 趙雷   | 1990-01-01 | 男    |   269 |    1 |
| 02   | 錢電   | 1990-12-21 | 男    |   210 |    3 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |   240 |    2 |
| 04   | 李云   | 1990-08-06 | 男    |   100 |    6 |
| 05   | 周梅   | 1991-12-01 | 女    |   163 |    5 |
| 06   | 吳蘭   | 1992-03-01 | 女    |    65 |    7 |
| 07   | 鄭竹   | 1989-07-01 | 女    |   187 |    4 |
| 09   | 張三   | 2017-12-20 | 女    |  NULL | NULL |
| 10   | 李四   | 2017-12-25 | 女    |  NULL | NULL |
| 11   | 李四   | 2017-12-30 | 女    |  NULL | NULL |
| 12   | 趙六   | 2017-01-01 | 女    |  NULL | NULL |
| 13   | 孫七   | 2018-01-01 | 女    |  NULL | NULL |
+------+--------+------------+-------+-------+------+

17.統(tǒng)計各科成績各分?jǐn)?shù)段人數(shù):課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所占百分比

select c.c_name,t.* from(select c_id,
sum(if(s_score <60,1,0)) as '[0,60)',
round(sum(if(s_score <60,1,0))/count(1)*100,2) as '[0,60)%',
sum(if(s_score >=60 and s_score<70,1,0)) as '[60,70)',
round(sum(if(s_score >=60 and s_score<70,1,0))/count(1)*100,2) as '[60,70)%',
sum(if(s_score >=70 and s_score<85,1,0)) as '[70,85)',
round(sum(if(s_score >=70 and s_score<85,1,0))/count(1)*100,2) as '[70,85)%',
sum(if(s_score >=85 and s_score<=100,1,0)) as '[85,100]',
round(sum(if(s_score >=85 and s_score<=100,1,0))/count(1)*100,2) as '[85,100]%'
from score group by c_id) as t left join course as c on c.c_id=t.c_id;

正確輸出:
+--------+------+--------+---------+---------+----------+---------+----------+----------+-----------+
| c_name | c_id | [0,60) | [0,60)% | [60,70) | [60,70)% | [70,85) | [70,85)% | [85,100] | [85,100]% |
+--------+------+--------+---------+---------+----------+---------+----------+----------+-----------+
| 語文   | 01   |      2 |   33.33 |       0 |     0.00 |       4 |    66.67 |        0 |      0.00 |
| 數(shù)學(xué)   | 02   |      1 |   16.67 |       1 |    16.67 |       1 |    16.67 |        3 |     50.00 |
| 英語   | 03   |      2 |   33.33 |       0 |     0.00 |       2 |    33.33 |        2 |     33.33 |
+--------+------+--------+---------+---------+----------+---------+----------+----------+-----------+

18.查詢各科成績前三名的記錄

select * from (select *,rank() over (partition by c_id order by s_score) as 'rank' from score)as t where t.rank<=3;

正確輸出:
+------+------+---------+------+
| s_id | c_id | s_score | rank |
+------+------+---------+------+
| 06   | 01   |      31 |    1 |
| 04   | 01   |      50 |    2 |
| 02   | 01   |      70 |    3 |
| 04   | 02   |      30 |    1 |
| 02   | 02   |      60 |    2 |
| 03   | 02   |      80 |    3 |
| 04   | 03   |      20 |    1 |
| 06   | 03   |      34 |    2 |
| 02   | 03   |      80 |    3 |
| 03   | 03   |      80 |    3 |
+------+------+---------+------+

19.查詢每門課程被選修的學(xué)生數(shù)

select c_id,count(1) as num from score group by c_id;

正確輸出:
+------+-----+
| c_id | num |
+------+-----+
| 01   |   6 |
| 02   |   6 |
| 03   |   6 |
+------+-----+

20.查詢出只選修兩門課程的學(xué)生學(xué)號和姓名

select s.s_id,s.s_name from student s inner join score sc on s.s_id=sc.s_id group by s_id having count(1)=2;

正確輸出:
+------+--------+
| s_id | s_name |
+------+--------+
| 05   | 周梅   |
| 06   | 吳蘭   |
| 07   | 鄭竹   |
+------+--------+

21.查詢男生、女生人數(shù)

select s_sex,count(1)as num from student group by s_sex;

正確輸出:
+-------+-----+
| s_sex | num |
+-------+-----+
| 男    |   4 |
| 女    |   8 |
+-------+-----+

☆22.查詢名字中含有「風(fēng)」字的學(xué)生信息

select * from student where s_name like '%風(fēng)%';
如果s_name是索引字段用%風(fēng)%會導(dǎo)致索引失效,最好用風(fēng)%
Q:那請問老師這種情況的查詢就要先搜索%風(fēng)再搜索風(fēng)%最后合并還是怎樣?

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |
+------+--------+------------+-------+

23.查詢同名同性學(xué)生名單,并統(tǒng)計同名人數(shù)

select s_name,s_sex,count(1)as num from student group by s_name,s_sex having num>1;

正確輸出:
+--------+-------+-----+
| s_name | s_sex | num |
+--------+-------+-----+
| 李四   | 女    |   2 |
+--------+-------+-----+

24.查詢 1990 年出生的學(xué)生名單

select * from student where year(s_birth)=1990;
如果s_birth是索引的話where字段用函數(shù)會導(dǎo)致索引失效,因此最好用where s_birth like "1990%"

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 01   | 趙雷   | 1990-01-01 | 男    |
| 02   | 錢電   | 1990-12-21 | 男    |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |
| 04   | 李云   | 1990-08-06 | 男    |
+------+--------+------------+-------+

25.查詢每門課程的平均成績,結(jié)果按平均成績降序排列,平均成績相同時,按課程編號升序排列

select c.c_id,c.c_name,round(avg(s_score),2) as avg from course c inner join score sc on sc.c_id=c.c_id group by c_id order by avg desc,c_id asc;

正確輸出:
+--------+------+-------+
| c_name | c_id | avg   |
+--------+------+-------+
| 數(shù)學(xué)   | 02   | 72.67 |
| 英語   | 03   | 68.50 |
| 語文   | 01   | 64.50 |
+--------+------+-------+

26.查詢平均成績大于等于 85 的所有學(xué)生的學(xué)號、姓名和平均成績

select s.s_id,s.s_name,round(avg(s_score),2) as avg from student s inner join score sc on sc.s_id=s.s_id group by s_id having avg(s_score)>=85;

正確輸出:
+--------+------+-------+
| s_name | s_id | avg   |
+--------+------+-------+
| 趙雷   | 01   | 89.67 |
| 鄭竹   | 07   | 93.50 |
+--------+------+-------+、

27.查詢課程名稱為「數(shù)學(xué)」,且分?jǐn)?shù)低于 60 的學(xué)生姓名和分?jǐn)?shù)

select s.s_name,sc.* from student as s inner join score as sc on sc.s_id=s.s_id inner join course c on c.c_id=sc.c_id where s_score<60 and c_name="數(shù)學(xué)";

正確輸出:
+--------+------+------+---------+
| s_name | s_id | c_id | s_score |
+--------+------+------+---------+
| 李云   | 04   | 02   |      30 |
+--------+------+------+---------+

28.查詢所有學(xué)生的課程及分?jǐn)?shù)情況(存在學(xué)生沒成績,沒選課的情況)

select s.*,sc.c_id,sc.s_score from student s left join score sc on s.s_id=sc.s_id;

正確輸出:
+------+--------+------------+-------+------+---------+
| s_id | s_name | s_birth    | s_sex | c_id | s_score |
+------+--------+------------+-------+------+---------+
| 01   | 趙雷   | 1990-01-01 | 男    | 01   |      80 |
| 01   | 趙雷   | 1990-01-01 | 男    | 02   |      90 |
| 01   | 趙雷   | 1990-01-01 | 男    | 03   |      99 |
| 02   | 錢電   | 1990-12-21 | 男    | 01   |      70 |
| 02   | 錢電   | 1990-12-21 | 男    | 02   |      60 |
| 02   | 錢電   | 1990-12-21 | 男    | 03   |      80 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    | 01   |      80 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    | 02   |      80 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    | 03   |      80 |
| 04   | 李云   | 1990-08-06 | 男    | 01   |      50 |
| 04   | 李云   | 1990-08-06 | 男    | 02   |      30 |
| 04   | 李云   | 1990-08-06 | 男    | 03   |      20 |
| 05   | 周梅   | 1991-12-01 | 女    | 01   |      76 |
| 05   | 周梅   | 1991-12-01 | 女    | 02   |      87 |
| 06   | 吳蘭   | 1992-03-01 | 女    | 01   |      31 |
| 06   | 吳蘭   | 1992-03-01 | 女    | 03   |      34 |
| 07   | 鄭竹   | 1989-07-01 | 女    | 02   |      89 |
| 07   | 鄭竹   | 1989-07-01 | 女    | 03   |      98 |
| 09   | 張三   | 2017-12-20 | 女    | NULL |    NULL |
| 10   | 李四   | 2017-12-25 | 女    | NULL |    NULL |
| 11   | 李四   | 2017-12-30 | 女    | NULL |    NULL |
| 12   | 趙六   | 2017-01-01 | 女    | NULL |    NULL |
| 13   | 孫七   | 2018-01-01 | 女    | NULL |    NULL |
+------+--------+------------+-------+------+---------+

29.查詢?nèi)魏我婚T課程成績在 70 分以上的姓名、課程名稱和分?jǐn)?shù)

select s.s_name,sc.* from student s left join score sc on s.s_id=sc.s_id where s_score>70;

正確輸出:
+--------+------+------+---------+
| s_name | s_id | c_id | s_score |
+--------+------+------+---------+
| 趙雷   | 01   | 01   |      80 |
| 趙雷   | 01   | 02   |      90 |
| 趙雷   | 01   | 03   |      99 |
| 錢電   | 02   | 03   |      80 |
| 孫風(fēng)   | 03   | 01   |      80 |
| 孫風(fēng)   | 03   | 02   |      80 |
| 孫風(fēng)   | 03   | 03   |      80 |
| 周梅   | 05   | 01   |      76 |
| 周梅   | 05   | 02   |      87 |
| 鄭竹   | 07   | 02   |      89 |
| 鄭竹   | 07   | 03   |      98 |
+--------+------+------+---------+

30.查詢不及格的課程

select score.* from score where s_score<60;

正確輸出:
+------+------+---------+
| s_id | c_id | s_score |
+------+------+---------+
| 04   | 01   |      50 |
| 04   | 02   |      30 |
| 04   | 03   |      20 |
| 06   | 01   |      31 |
| 06   | 03   |      34 |
+------+------+---------+

31.查詢課程編號為 01 且課程成績在 80 分以上的學(xué)生的學(xué)號和姓名

select s.s_name,sc.* from student s inner join score sc on s.s_id=sc.s_id where c_id="01" and s_score>80;

正確輸出:
Empty set

32.求每門課程的學(xué)生人數(shù)

select c_id,count(1) as num from score group by c_id;

正確輸出:
+------+-----+
| c_id | num |
+------+-----+
| 01   |   6 |
| 02   |   6 |
| 03   |   6 |
+------+-----+

☆33.假設(shè)成績不重復(fù),查詢選修「張三」老師所授課程的學(xué)生中,成績最高的學(xué)生信息及其成績

select s.*,sc.c_id,sc.s_score from student s inner join score sc on s.s_id=sc.s_id and c_id=(select c_id from teacher t inner join course c on t.t_id=c.t_id and t.t_name="張三") order by s_score desc limit 1;

正確輸出:
+------+--------+------------+-------+------+---------+
| s_id | s_name | s_birth    | s_sex | c_id | s_score |
+------+--------+------------+-------+------+---------+
| 01   | 趙雷   | 1990-01-01 | 男    | 02   |      90 |
+------+--------+------------+-------+------+---------+

和27題相似的問題,因為27題加入一個子查詢比較快,所以我試圖在33題中也加入一個,相比直接 inner join確實快了
+----------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|       47 | 0.00080375 | select s.*,sc.c_id,sc.s_score from score sc inner join student s on s.s_id=sc.s_id inner join course c on sc.c_id=c.c_id inner join teacher t on c.t_id=t.t_id and t.t_name='????' order by sc.s_score desc limit 1 |
|       48 | 0.00072225 | select s.*,sc.c_id,sc.s_score from student s inner join score sc on s.s_id=sc.s_id and c_id=(select c_id from teacher t inner join course c on t.t_id=c.t_id and t.t_name="????") order by s_score desc limit 1     |
+----------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

子查詢explain:
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref          | rows | filtered | Extra                                              |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+----------------------------------------------------+
|  1 | PRIMARY     | sc    | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |   18 |    10.00 | Using where; Using filesort                        |
|  1 | PRIMARY     | s     | NULL       | eq_ref | PRIMARY       | PRIMARY | 62      | test.sc.s_id |    1 |   100.00 | NULL                                               |
|  2 | SUBQUERY    | t     | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |    3 |    33.33 | Using where                                        |
|  2 | SUBQUERY    | c     | NULL       | ALL    | NULL          | NULL    | NULL    | NULL         |    3 |    33.33 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+----------------------------------------------------+

join explain:
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref          | rows | filtered | Extra                                              |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+----------------------------------------------------+
|  1 | SIMPLE      | t     | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |    3 |    33.33 | Using where; Using temporary; Using filesort       |
|  1 | SIMPLE      | c     | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |    3 |    33.33 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | sc    | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |   18 |    10.00 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | s     | NULL       | eq_ref | PRIMARY       | PRIMARY | 62      | test.sc.s_id |    1 |   100.00 | NULL                                               |
+----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+----------------------------------------------------+

34.假設(shè)成績有重復(fù)的情況下,查詢選修「張三」老師所授課程的學(xué)生中,成績最高的學(xué)生信息及其成績

select * from(select s.*,sc.c_id,sc.s_score,rank() over (order by s_score desc) as 'ran' from student s inner join score sc on s.s_id=sc.s_id and c_id=(select c_id from teacher t inner join course c on t.t_id=c.t_id and t.t_name="張三"))as t where ran=1;

正確輸出:
+------+--------+------------+-------+------+---------+-----+
| s_id | s_name | s_birth    | s_sex | c_id | s_score | ran |
+------+--------+------------+-------+------+---------+-----+
| 01   | 趙雷   | 1990-01-01 | 男    | 02   |      90 |   1 |
+------+--------+------------+-------+------+---------+-----+

35.查詢不同課程成績相同的學(xué)生的學(xué)生編號、課程編號、學(xué)生成績

select * from score sc inner join score sc2 on sc.s_id=sc2.s_id where sc.c_id!=sc2.c_id and sc.s_score=sc2.s_score group by sc.s_id ,sc.c_id;

正確輸出:
+------+------+---------+------+------+---------+
| s_id | c_id | s_score | s_id | c_id | s_score |
+------+------+---------+------+------+---------+
| 03   | 01   |      80 | 03   | 02   |      80 |
| 03   | 02   |      80 | 03   | 01   |      80 |
| 03   | 03   |      80 | 03   | 01   |      80 |
+------+------+---------+------+------+---------+

36.查詢每門功成績最好的前兩名

select * from (select s_id,s_score,c_id,rank() over (partition by c_id order by s_score desc) as ran from score)as t where ran<=2 ;

正確輸出:
+------+---------+------+-----+
| s_id | s_score | c_id | ran |
+------+---------+------+-----+
| 01   |      80 | 01   |   1 |
| 03   |      80 | 01   |   1 |
| 01   |      90 | 02   |   1 |
| 07   |      89 | 02   |   2 |
| 01   |      99 | 03   |   1 |
| 07   |      98 | 03   |   2 |
+------+---------+------+-----+

37.統(tǒng)計每門課程的學(xué)生選修人數(shù)(超過 5 人的課程才統(tǒng)計)。

select c_id,count(1) as num from score group by c_id having num>5;

正確輸出:
+------+-----+
| c_id | num |
+------+-----+
| 01   |   6 |
| 02   |   6 |
| 03   |   6 |
+------+-----+

38.檢索至少選修兩門課程的學(xué)生學(xué)號

select s_id from score group by s_id having count(1)>=2;

正確輸出:
+------+
| s_id |
+------+
| 01   |
| 02   |
| 03   |
| 04   |
| 05   |
| 06   |
| 07   |
+------+

39.查詢選修了全部課程的學(xué)生信息

select s.* from student s inner join (select s_id,count(1)as num from score group by s_id) as t on s.s_id=t.s_id where num=(select count(1) from course);

#如果用having+子查詢效率非常低

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 01   | 趙雷   | 1990-01-01 | 男    |
| 02   | 錢電   | 1990-12-21 | 男    |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |
| 04   | 李云   | 1990-08-06 | 男    |
+------+--------+------------+-------+

40.查詢各學(xué)生的年齡,只按年份來算

select *,year(now())-year(s_birth)as age from student;

正確輸出:
+------+--------+------------+-------+------+
| s_id | s_name | s_birth    | s_sex | age  |
+------+--------+------------+-------+------+
| 01   | 趙雷   | 1990-01-01 | 男    |   30 |
| 02   | 錢電   | 1990-12-21 | 男    |   30 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |   30 |
| 04   | 李云   | 1990-08-06 | 男    |   30 |
| 05   | 周梅   | 1991-12-01 | 女    |   29 |
| 06   | 吳蘭   | 1992-03-01 | 女    |   28 |
| 07   | 鄭竹   | 1989-07-01 | 女    |   31 |
| 09   | 張三   | 2017-12-20 | 女    |    3 |
| 10   | 李四   | 2017-12-25 | 女    |    3 |
| 11   | 李四   | 2017-12-30 | 女    |    3 |
| 12   | 趙六   | 2017-01-01 | 女    |    3 |
| 13   | 孫七   | 2018-01-01 | 女    |    2 |
+------+--------+------------+-------+------+

41.按照出生日期來算,當(dāng)前月日 < 出生年月的月日則,年齡減一

select *,timestampdiff(year,s_birth,now()) as age from student;

正確輸出:
+------+--------+------------+-------+------+
| s_id | s_name | s_birth    | s_sex | age  |
+------+--------+------------+-------+------+
| 01   | 趙雷   | 1990-01-01 | 男    |   30 |
| 02   | 錢電   | 1990-12-21 | 男    |   29 |
| 03   | 孫風(fēng)   | 1990-05-20 | 男    |   29 |
| 04   | 李云   | 1990-08-06 | 男    |   29 |
| 05   | 周梅   | 1991-12-01 | 女    |   28 |
| 06   | 吳蘭   | 1992-03-01 | 女    |   27 |
| 07   | 鄭竹   | 1989-07-01 | 女    |   30 |
| 09   | 張三   | 2017-12-20 | 女    |    2 |
| 10   | 李四   | 2017-12-25 | 女    |    2 |
| 11   | 李四   | 2017-12-30 | 女    |    2 |
| 12   | 趙六   | 2017-01-01 | 女    |    3 |
| 13   | 孫七   | 2018-01-01 | 女    |    2 |
+------+--------+------------+-------+------+

☆42.查詢本周過生日的學(xué)生

Q:這種方法好像也會讓索引失效,不知道有沒有更好的方法?

select * from student where weekofyear(date_format(s_birth,"2020-%m-%d"))=weekofyear(now());

Empty set (0.00 sec)

43.查詢下周過生日的學(xué)生

select * from student where weekofyear(date_format(s_birth,"2020-%m-%d"))=weekofyear(now())+1;

Empty set (0.00 sec)

44.查詢本月過生日的學(xué)生

select * from student where s_birth like concat("____-0",month(now()),"%");

正確輸出:
+------+--------+------------+-------+
| s_id | s_name | s_birth    | s_sex |
+------+--------+------------+-------+
| 01   | 趙雷   | 1990-01-01 | 男    |
| 12   | 趙六   | 2017-01-01 | 女    |
| 13   | 孫七   | 2018-01-01 | 女    |
+------+--------+------------+-------+

45.查詢下月過生日的學(xué)生

select * from student where s_birth like concat("____-0",month(now())+1,"%");

Empty set (0.00 sec)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內(nèi)容