140 MySQL多表練習

1、練習

1.1 表與數據準備

company.employee
    員工id      id                  int             
    姓名        emp_name            varchar
    性別        sex                 enum
    年齡        age                 int
    入職日期     hire_date           date
    崗位        post                varchar
    職位描述     post_comment        varchar
    薪水        salary              double
    辦公室       office              int
    部門編號     depart_id           varchar

# 建立員工信息表
create table employee(id int primary key auto_increment,
                      emp_name varchar(64) not null,
                      sex enum('male','female')not null default 'male',
                      age int unsigned not null default 25,
                      hire_data date not null,
                      post varchar(64),
                      post_comment varchar(100),
                      salary decimal(7,2),
                      office int,
                      depart_id varchar(64) default '銷售部'              
);

# 查看錶結構
mysql> desc employee;
+--------------+-----------------------+------+-----+-----------+----------------+
| Field        | Type                  | Null | Key | Default   | Extra          |
+--------------+-----------------------+------+-----+-----------+----------------+
| id           | int(11)               | NO   | PRI | NULL      | auto_increment |
| emp_name     | varchar(64)           | NO   |     | NULL      |                |
| sex          | enum('male','female') | NO   |     | male      |                |
| age          | int(10) unsigned      | NO   |     | 25        |                |
| hire_data    | date                  | NO   |     | NULL      |                |
| post         | varchar(64)           | YES  |     | NULL      |                |
| post_comment | varchar(100)          | YES  |     | NULL      |                |
| salary       | decimal(7,2)          | YES  |     | NULL      |                |
| office       | int(11)               | YES  |     | NULL      |                |
| depart_id    | varchar(64)           | YES  |     | 銷售部    |                |
+--------------+-----------------------+------+-----+-----------+----------------+


# 插入記錄
# 三個部門:教學部,銷售部,運營部
insert into employee(emp_name,sex,age,hire_data,post,post_comment,salary,office,depart_id)values
('張三','male','25','20170301','teacher','教學',7843.2,401,'教學部'),#如下全是教學部
('李四','male','26','20150411','teacher','教學',6543.1,401,'教學部'),
('王二','female','24','20170621','teacher','教學',6631.5,401,'教學部'),
('劉五','male','36','20150411','teacher','教學',6200.2,401,'教學部'),
('陳雨','female','18','20191119','teacher','教學',8564.5,401,'教學部'),
('劉強','male','42','20140506','teacher','教學',9543,401,'教學部'),


('陳二','male','28','20150301','sale','銷售',6843.2,402,'銷售部'),#如下全是銷售部
('汪雨','male','20','20140411','sale','銷售',5543.1,402,'銷售部'),
('劉高','female','24','20150621','sale','銷售',5631.5,402,'銷售部'),
('劉七','male','18','20180411','sale','銷售',4200.2,402,'銷售部'),
('張雨','female','18','20131119','sale','銷售',9564.5,402,'銷售部'),
('許嵩','male','26','20120506','sale','銷售',9543,402,'銷售部'),


('張天','male','48','20150401','operation','運營維護',8843.2,403,'運營部'),#如下全是運營部
('梁田','male','38','20140811','operation','運營維護',7543.1,403,'運營部'),
('陳曉','female','24','20160521','operation','運營維護',5631.5,403,'運營部'),
('黃磊','male','18','20150411','operation','運營維護',4200.2,403,'運營部'),
('禾苗','female','18','20130129','operation','運營維護',9564.5,403,'運營部'),
('楊宇','female','42','20120506','operation','運營維護',9543,403,'運營部');

2.2 題目

1.查詢出每一個部門每次最新入職的那位員工sql

# 註解,其實就是須要連表去查詢,首先須要先按部門分組查詢每一個組入職日期,再去和真實的表進行內鏈接,也就浙江一個sql語句的結果做爲臨時表去和真實表進行內鏈接,把兩個表有關聯的數據拿出來
select 
    * 
from 
    employee 
JOIN(
    select 
        post,
        max(hire_data) max_hire_data
    from 
        employee 
    group by
        post
)as t2 on t1.post = t2.post 
where 
employee.hire_data = t2.max_hire_data;

2、綜合練習

2.1 數據準備

# 先建立一個班級表,並導入數據
CREATE TABLE class (
  cid int(11) NOT NULL AUTO_INCREMENT,
  caption varchar(32) NOT NULL,
  PRIMARY KEY (cid)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

BEGIN;
INSERT INTO class VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');
COMMIT;

# 建立一個學生表並導入數據,和班級的cid建立外鍵
CREATE TABLE student (
  sid int(11) NOT NULL AUTO_INCREMENT,
  gender char(1) NOT NULL,
  class_id int(11) NOT NULL,
  sname varchar(32) NOT NULL,
  PRIMARY KEY (sid),
  fk_class (class_id),
  FOREIGN KEY (class_id) REFERENCES class (cid)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

BEGIN;
INSERT INTO student VALUES ('1', '男', '1', '理解'), ('2', '女', '1', '鋼蛋'), ('3', '男', '1', '張三'), ('4', '男', '1', '張一'), ('5', '女', '1', '張二'), ('6', '男', '1', '張四'), ('7', '女', '2', '鐵錘'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '劉三'), ('14', '男', '3', '劉一'), ('15', '女', '3', '劉二'), ('16', '男', '3', '劉四');
COMMIT;


# 建立一個教師表,並插入數據
CREATE TABLE teacher (
  tid int(11) NOT NULL AUTO_INCREMENT,
  tname varchar(32) NOT NULL,
  PRIMARY KEY (tid)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

BEGIN;
INSERT INTO teacher VALUES ('1', '張磊老師'), ('2', '李平老師'), ('3', '劉海燕老師'), ('4', '朱雲海老師'), ('5', '李傑老師');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;


# 建立一個課程表,和教師表的tid創建外鍵,對應的教師教授哪一門課程
CREATE TABLE course (
  cid int(11) NOT NULL AUTO_INCREMENT,
  cname varchar(32) NOT NULL,
  teacher_id int(11) NOT NULL,
  PRIMARY KEY (cid),
  KEY fk_course_teacher (teacher_id),
  FOREIGN KEY (teacher_id) REFERENCES teacher (tid)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

BEGIN;
INSERT INTO course VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '體育', '3'), ('4', '美術', '2');
COMMIT;


# 建立學生成績表,和課程信息的課程id添加外鍵,,和學生的sid添加外鍵,每一個學生的每門課程的成績,並插入數據
CREATE TABLE score (
  sid int(11) NOT NULL AUTO_INCREMENT,
  student_id int(11) NOT NULL,
  course_id int(11) NOT NULL,
  num int(11) NOT NULL,
  PRIMARY KEY (sid),
  KEY fk_score_student (student_id`),
  KEY fk_score_course (course_id`),
  FOREIGN KEY (course_id) REFERENCES course (cid),
  FOREIGN KEY (student_id) REFERENCES student (sid)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;

BEGIN;
INSERT INTO score VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
COMMIT;

2.2 表的數據

# 班級表數據
+-----+--------------+
| cid | caption      |
+-----+--------------+
|   1 | 三年二班     |
|   2 | 三年三班     |
|   3 | 一年二班     |
|   4 | 二年九班     |
+-----+--------------+

# 學生表數據
+-----+--------+----------+--------+
| sid | gender | class_id | sname  |
+-----+--------+----------+--------+
|   1 | 男     |        1 | 理解   |
|   2 | 女     |        1 | 鋼蛋   |
|   3 | 男     |        1 | 張三   |
|   4 | 男     |        1 | 張一   |
|   5 | 女     |        1 | 張二   |
|   6 | 男     |        1 | 張四   |
|   7 | 女     |        2 | 鐵錘   |
|   8 | 男     |        2 | 李三   |
|   9 | 男     |        2 | 李一   |
|  10 | 女     |        2 | 李二   |
|  11 | 男     |        2 | 李四   |
|  12 | 女     |        3 | 如花   |
|  13 | 男     |        3 | 劉三   |
|  14 | 男     |        3 | 劉一   |
|  15 | 女     |        3 | 劉二   |
|  16 | 男     |        3 | 劉四   |
+-----+--------+----------+--------+

# 教師表數據
+-----+-----------------+
| tid | tname           |
+-----+-----------------+
|   1 | 張磊老師        |
|   2 | 李平老師        |
|   3 | 劉海燕老師      |
|   4 | 朱雲海老師      |
|   5 | 李傑老師        |
+-----+-----------------+

# 課程表數據
+-----+--------+------------+
| cid | cname  | teacher_id |
+-----+--------+------------+
|   1 | 生物   |          1 |
|   2 | 物理   |          2 |
|   3 | 體育   |          3 |
|   4 | 美術   |          2 |
+-----+--------+------------+

# 成績表信息因爲過多,此處就不展現了

2.3 練習題目

  1. 查詢男生、女生的人數;函數

    # 按照年齡分組
    select gender,count(sid) from studnet group by gender;
  2. 查詢姓「張」的學生名單;post

    # 用類似符合關鍵字like來從學生表裏查詢學生的姓名,條件是姓名第一個字是張like '張%'
    select sname  from student where sname like '張%';
  3. 課程平均分從高到低顯示code

    # 將成績表score中將課程號進行分組,而後利用聚合函數對每組的分數進行avg(),以後order by
    select course_id 課程號,avg(num) 平均分 from score group by course_id order by 平均分 desc;
  4. 查詢有課程成績小於60分的同窗的學號、姓名;排序

    # 先查詢成績表的學生成績小於60的學生,而後對其學生學號分組
    # 將這個sql查詢語句的結果做爲一個臨時表,和學生表進行內鏈接,將學生的學號和姓名提取
    select 
        sid,
        sname 
    from 
     student 
    join 
     (select student_id,GROUP_CONCAT(num) from score where num<60  GROUP by student_id) 
    as t1 on t1.student_id=student.sid;
  5. 查詢至少有一門課與學號爲1的同窗所學課程相同的同窗的學號和姓名;ci

    # 先對成績表中的學號爲1的學生的課程號拿出:select course_id from score where student_id=1
    # 對成績表中學號大於1的學生課程號進行條件匹配,比較是否在上一個系查詢的結果中,把上一個子查詢做爲條件
    # 拿出學號大於1且課程號在學號1的選擇的課程中的學生學號,以後和學生表進行內鏈接,取出這些學生的學號姓名
    select 
     sid,sname
    from student 
    join 
     (select student_id,GROUP_CONCAT(course_id) 
         from 
             score
      where 
             course_id in (select course_id from score where student_id=1) AND student_id>1 
      GROUP BY
             student_id) as t1 
    on student.sid=t1.student_id;
  6. 查詢出只選修了一門課程的所有學生的學號和姓名;rem

    # 先對成表中學生的學號進行排序,用count()聚合函數將統計學生的選擇的課程數量,用having對聚合函數後的結果進行篩選
    # 拿到只選擇1門課程的學生學號,和學生表student進行內鏈接
    select sid,sname from student join (select student_id,count(course_id) from score group by student_id having count(course_id)=1) as t1 on student.sid=t1.student_id;
  7. 查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;it

    select course_id 課程ID,max(num) 最高分,min(num)最低分 from score GROUP BY course_id;
  8. 查詢課程編號「2」的成績比課程編號「1」課程低的全部同窗的學號、姓名;io

    # 先查詢成績表裏都選擇課程1的全部學生--子查詢----臨時表t1
    select t1.student_id from (select student_id,course_id,num from score where  course_id=1
    
    # 再查詢成績表裏都選擇課程2的全部學生--子查詢----臨時表t2
    select student_id,course_id,num from score where  course_id=2                           
    # 經過t1和t2表進行內鏈接,獲得有關聯的數據學生的學號
    from student
     where sid in 
     (select t1.student_id from (select student_id,course_id,num from score where  course_id=1)as t1 
    left join (select student_id,course_id,num from score where  course_id=2)as t2 on t1.student_id=t2.student_id 
    
    
    # 拿到符合條件的學號的查詢結果,做爲去學生表student裏查詢學生學號和姓名的條件
    select sid,sname
    from student
     where sid in 
     (select t1.student_id from (select student_id,course_id,num from score where  course_id=1)as t1 
    left join (select student_id,course_id,num from score where  course_id=2)as t2 on t1.student_id=t2.student_id 
    where t1.num>t2.num)
  9. 查詢「生物」課程比「物理」課程成績高的全部學生的學號;

    select t1.sid from(
        (select * from score where course_id in (select cid from course where cname='生        物')) as t1
    left join 
        (select * from score where course_id in (select cid from course where cname='物       理')) as t2 
    on t1.student_id=t2.student_id)
    where t1.num>t2.num;
  10. 查詢平均成績大於60分的同窗的學號和平均成績;

    # 先查詢學生的平均成績
    select student_id,avg(num) 平均成績 from score group by student_id 
    # 篩選平均成績大於60的學生學號
  11. 查詢全部同窗的學號、姓名、選課數、總成績;

    # 先查詢全部學生的學號,選課數,總成績
    select student_id,count(course_id),sum(num) from score group by student_id
    # 再與學生表student進行內鏈接,查詢學生的名字
    select student.sid,student.sname,選課數,總分 from student 
    join
        (select student_id,count(course_id) 選課數,sum(num) 總分 from score group by         student_id)as t1
    where student.sid=t1.student_id;
  12. 查詢姓「李」的老師的個數;

    select count(tid) from teacher  where tname like '李%'
  13. 查詢沒學過「張磊老師」課的同窗的學號、姓名;

    # 先查詢張磊老師老師的編號:(select tid from teacher where tname='張磊老師')
    # 而後做爲查詢張磊老師所帶的課程號的條件:select cid from course where teacher_id in (select tid from teacher where tname='張磊老師'))
    
    #從成績表裏查詢全部學生的課程id=張磊老師所帶課程的課程號的學生學號
    select student_id from score where course_id=(select cid from course where teacher_id in (select tid from teacher where tname='張磊老師')))
    
    
    # 將上面的一個結果做爲總體的一個執行結果至關因而一個表,在去學生表裏查詢學生學號在這個範圍內的姓名和學號
    select sid sname from student where sid not in (select student_id from score where course_id=(select cid from course where teacher_id in (select tid from teacher where tname='張磊老師')))
  14. 查詢學過「1」而且也學過編號「2」課程的同窗的學號、姓名;

    # 先從成績表裏查詢所欲選擇課程1的學生
    (select t1.student_id from (select student_id from score where course_id=1)as t1
    # 再從成績表裏選擇所欲選擇課程2的學生
    (select student_id from score where course_id=1)as t2
    # 把這兩個表進行內鏈接取出有用的學生學號
    select t1.student_id from (select student_id from score where course_id=1)as t1 join (select student_id from score where course_id=1)as t2 on t1.student_id=t2.student_id)
    
    # 將全部學號取出以後,再從學生表裏進行條件匹配
    select sid,sname from student where sid in (select t1.student_id from (select student_id from score where course_id=1)as t1 join (select student_id from score where course_id=1)as t2 on t1.student_id=t2.student_id)
  15. 查詢學過「李平老師」所教的全部課的同窗的學號、姓名;

    select sid,sname from student where sid in (select student_id from score where course_id=(select tid from teacher where tname='李平老師'))
相關文章
相關標籤/搜索