高级查询(mysql)

高级查询

> 其他+V:w891123884

MySQL提供了几种高级查询语句,用于复杂场所下进行多表一起查询,这样就会用到内连接查询、外连接查询、自然连接查询、交叉连接查询和联合查询,以满足日常业务查询的需求,从而更能体会MySQL强大的功能。

1、内连接查询

SELECT*FROM 左表 [INNER] JOIN 右表 ON 左表.字段=右表.字段;

mysql> select*from user;
+----+-------+------+------+----------+---------+-----------+
| id | name  | sex  | age  | password | phone   | loginname |
+----+-------+------+------+----------+---------+-----------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |
+----+-------+------+------+----------+---------+-----------+
5 rows in set (0.00 sec)

mysql> use shop;
Database changed
mysql> create table score(
    -> id int not null,
    -> score nvarchar(233),
    -> grage nvarchar(233),
    -> primary key(id));
Query OK, 0 rows affected (0.07 sec)


mysql> insert into score values(1,'88','优秀');
Query OK, 1 row affected (0.02 sec)

mysql> insert into score values(2,'70','中等');
Query OK, 1 row affected (0.02 sec)

mysql> insert into score values(3,'60','不优秀');
Query OK, 1 row affected (0.02 sec)

mysql> select*from score;
+----+-------+--------+
| id | score | grage  |
+----+-------+--------+
|  1 | 88    | 优秀   |
|  2 | 70    | 中等   |
|  3 | 60    | 不优秀 |
+----+-------+--------+
3 rows in set (0.02 sec)

mysql> select*from user u inner join score s on u.id=s.id;  
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
3 rows in set (0.02 sec)

mysql> select*from user u join score s on u.id=s.id;
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
3 rows in set (0.00 sec)

mysql> select u.id uid,u.name,u.sex,u.age,s.score,s.grage from user u join score s on u.id=s.id; 
+-----+-------+------+------+-------+--------+
| uid | name  | sex  | age  | score | grage  |
+-----+-------+------+------+-------+--------+
|   1 | david || 19   | 88    | 优秀   |
|   2 | 小明  || 22   | 70    | 中等   |
|   3 | 小花  || 21   | 60    | 不优秀 |
+-----+-------+------+------+-------+--------+
3 rows in set (0.00 sec)

mysql> select u.id uid,u.name,u.sex,u.age,s.score,s.grage from user u join score s u.id=s.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'u.id=s.id' at line 1
mysql> select*from user u join score s;
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  2 | 70    | 中等   |
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  3 | 60    | 不优秀 |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  3 | 60    | 不优秀 |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  1 | 88    | 优秀   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  1 | 88    | 优秀   |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  2 | 70    | 中等   |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  3 | 60    | 不优秀 |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  1 | 88    | 优秀   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  2 | 70    | 中等   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  3 | 60    | 不优秀 |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
15 rows in set (0.00 sec)

mysql> select*from user u inner join score s on u.id=s.id;
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
3 rows in set (0.00 sec)

mysql>  select*from user u inner join score s on u.id>s.id;
+----+------+------+------+----------+---------+-----------+----+-------+--------+
| id | name | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+------+------+------+----------+---------+-----------+----+-------+--------+
|  2 | 小明 || 22   | 123456   | 444444  | hfhhj     |  1 | 88    | 优秀   |
|  3 | 小花 || 21   | 123456   | 6666666 | hfhhkj    |  1 | 88    | 优秀   |
|  3 | 小花 || 21   | 123456   | 6666666 | hfhhkj    |  2 | 70    | 中等   |
|  4 | 小敏 || 25   | 123456   | 8888888 | hfhhj     |  1 | 88    | 优秀   |
|  4 | 小敏 || 25   | 123456   | 8888888 | hfhhj     |  2 | 70    | 中等   |
|  4 | 小敏 || 25   | 123456   | 8888888 | hfhhj     |  3 | 60    | 不优秀 |
|  5 | 小华 || 26   | 123456   | 999999  | hfhhj     |  1 | 88    | 优秀   |
|  5 | 小华 || 26   | 123456   | 999999  | hfhhj     |  2 | 70    | 中等   |
|  5 | 小华 || 26   | 123456   | 999999  | hfhhj     |  3 | 60    | 不优秀 |
+----+------+------+------+----------+---------+-----------+----+-------+--------+
9 rows in set (0.00 sec)

2、外连接查询

SELECT * FROM 左表 LEFT/RIGHT JOIN 右表 ON 左表.字段=右表.字段;

mysql> select*from user u left join score s on u.id=s.id;+----+-------+------+------+----------+---------+-----------+------+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id   | score | grage  |
+----+-------+------+------+----------+---------+-----------+------+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |    1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |    2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |    3 | 60    | 不优秀 |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     | NULL | NULL  | NULL   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     | NULL | NULL  | NULL   |
+----+-------+------+------+----------+---------+-----------+------+-------+--------+
5 rows in set (0.02 sec)

mysql> select*from user u right join score s on u.id=s.id;+------+-------+------+------+----------+---------+-----------+----+-------+--------+
| id   | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+------+-------+------+------+----------+---------+-----------+----+-------+--------+
|    1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|    2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|    3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
+------+-------+------+------+----------+---------+-----------+----+-------+--------+
3 rows in set (0.00 sec)

3、交叉连接查询

SELECT *左表 CROSS JOIN 右表 或 FROM 左表,右表;

mysql> select*from user cross join score;交叉连接
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  2 | 70    | 中等   |
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  3 | 60    | 不优秀 |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  3 | 60    | 不优秀 |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  1 | 88    | 优秀   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  1 | 88    | 优秀   |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  2 | 70    | 中等   |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  3 | 60    | 不优秀 |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  1 | 88    | 优秀   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  2 | 70    | 中等   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  3 | 60    | 不优秀 |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
15 rows in set (0.00 sec)

mysql> select*from user,score;from两个表
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
| id | name  | sex  | age  | password | phone   | loginname | id | score | grage  |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  1 | 88    | 优秀   |
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  2 | 70    | 中等   |
|  1 | david || 19   | 123456   | 5555555 | hfhhj     |  3 | 60    | 不优秀 |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  1 | 88    | 优秀   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  2 | 70    | 中等   |
|  2 | 小明  || 22   | 123456   | 444444  | hfhhj     |  3 | 60    | 不优秀 |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  1 | 88    | 优秀   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  2 | 70    | 中等   |
|  3 | 小花  || 21   | 123456   | 6666666 | hfhhkj    |  3 | 60    | 不优秀 |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  1 | 88    | 优秀   |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  2 | 70    | 中等   |
|  4 | 小敏  || 25   | 123456   | 8888888 | hfhhj     |  3 | 60    | 不优秀 |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  1 | 88    | 优秀   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  2 | 70    | 中等   |
|  5 | 小华  || 26   | 123456   | 999999  | hfhhj     |  3 | 60    | 不优秀 |
+----+-------+------+------+----------+---------+-----------+----+-------+--------+
15 rows in set (0.00 sec)

4、联合查询

SELECT column_name FROM table1
UNION (all)
SELECT column_name FROM table2;

mysql> select id from user;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
+----+
5 rows in set (0.00 sec)

mysql> select id from user       集合
    -> union
    -> select id from score;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
+----+
5 rows in set (0.02 sec)

mysql> select id,name from user
    -> union
    -> select id,score from score;
+----+-------+
| id | name  |
+----+-------+
|  1 | david |
|  2 | 小明  |
|  3 | 小花  |
|  4 | 小敏  |
|  5 | 小华  |
|  1 | 88    |
|  2 | 70    |
|  3 | 60    |
+----+-------+
8 rows in set (0.00 sec)

高级查询(mysql)_第1张图片

你可能感兴趣的:(mysql,mysql)