-
Notifications
You must be signed in to change notification settings - Fork 0
/
关联查询.sql
69 lines (54 loc) · 1.5 KB
/
关联查询.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
CREATE DATABASE IF NOT EXISTS test;
USE test;
//学生
CREATE TABLE IF NOT EXISTS student(
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'id',
`name` VARCHAR(20),
`age` INT,
`grade` INT,
PRIMARY KEY(id)
) ENGINE = INNODB CHARSET = utf8;
//年级
CREATE TABLE IF NOT EXISTS grade(
id INT NOT NULL AUTO_INCREMENT,
gname VARCHAR(20),
PRIMARY KEY(id)
)ENGINE = INNODB CHARSET = utf8;
//分数
CREATE TABLE IF NOT EXISTS result(
sid INT,
score INT
)ENGINE = INNODB CHARSET = utf8;
INSERT INTO student(`name`, grade)VALUES("zhangshan",1),("lisi",1),("wangwu",2),("zhaoliu",2),("xiaoming",3);
SELECT * FROM student;
UPDATE student SET age = 18;
INSERT INTO grade(gname)VALUES('grade1'),('grade2'),('grade3'),('grade4');
SELECT * FROM grade;
INSERT INTO result(sid,score)VALUES(1,100),(2,80),(3,90),(4,50);
SELECT * FROM result;
//笛卡尔积
SELECT * FROM student,grade,result;
//查询每个年级有哪些学生
SELECT gname, `name` FROM grade g
INNER JOIN student s
ON s.grade = g.id;
SELECT gname, `name` FROM grade g
LEFT JOIN student s
ON s.grade = g.id;
SELECT gname, `name` FROM grade g
RIGHT JOIN student s
ON s.grade = g.id;
SELECT gname, `name` FROM grade g,student s WHERE s.grade = g.id;
//查询每个年级每个同学的成绩
SELECT gname,`name`,score FROM grade g
INNER JOIN student s
ON s.grade = g.id
INNER JOIN result r
ON s.id = r.sid;
SELECT gname,`name`,score FROM grade g
LEFT JOIN student s
ON s.grade = g.id
LEFT JOIN result r
ON s.id = r.sid;
//子查询
SELECT gname,`name`,score FROM