您的位置:首页 > 数据库 > MySQL

MySQL-Join

2017-01-17 18:34 309 查看

环境准备

三个table:书籍信息book、作者信息author、出版社信息publish。

create database temp;
use temp;
create table book (id int not null auto_increment, name varchar(20) not null, author int not null, pub int not null, price int default 0, primary key (id));
create table author (id int not null auto_increment, name varchar(20) not null, age int, primary key (id));
create table publish (id int not null auto_increment, name varchar(20) not null, city varchar(20), primary key (id));


增加测试数据:

insert into author (name, age) values ("Tom", 20);
insert into author (name, age) values ("Jerry", 25);

insert into publish (name, city) values ("PUB Tigger", "BJ");
insert into publish (name, city) values ("PUB Lion", "SH");
insert into publish (name, city) values ("PUB Rabbit", "SZ");

insert into book (name, author, pub, price) values ("Good Mood", 1, 1, 110);
insert into book (name, author, pub, price) values ("Rainy Day", 2, 3, 230);
insert into book (name, author, pub, price) values ("On the Road", 2, 2, 220);


验证:

MariaDB [temp]> select * from book;
+----+-------------+--------+-----+-------+
| id | name        | author | pub | price |
+----+-------------+--------+-----+-------+
|  1 | Good Mood   |      1 |   1 |   110 |
|  2 | Rainy Day   |      2 |   3 |   230 |
|  3 | On the Road |      2 |   2 |   220 |
+----+-------------+--------+-----+-------+
3 rows in set (0.00 sec)

MariaDB [temp]> select * from author;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | Tom   |   20 |
|  2 | Jerry |   25 |
+----+-------+------+
2 rows in set (0.00 sec)

MariaDB [temp]> select * from publish;
+----+------------+------+
| id | name       | city |
+----+------------+------+
|  1 | PUB Tigger | BJ   |
|  2 | PUB Lion   | SH   |
|  3 | PUB Rabbit | SZ   |
+----+------------+------+
3 rows in set (0.00 sec)


Join

根据三个表,查询完整的信息;Join有两种使用方式。

MariaDB [temp]> select book.name, author.name as author, publish.name as publish, publish.city, book.price from book, author, publish where book.author=author.id and book.pub=publish.id;
+-------------+--------+------------+------+-------+
| name        | author | publish    | city | price |
+-------------+--------+------------+------+-------+
| Good Mood   | Tom    | PUB Tigger | BJ   |   110 |
| Rainy Day   | Jerry  | PUB Rabbit | SZ   |   230 |
| On the Road | Jerry  | PUB Lion   | SH   |   220 |
+-------------+--------+------------+------+-------+
3 rows in set (0.00 sec)

MariaDB [temp]> select book.name, author.name as author, publish.name as publish, publish.city, book.price from book inner join author inner join publish on book.author=author.id and book.pub=publish.id;
+-------------+--------+------------+------+-------+
| name        | author | publish    | city | price |
+-------------+--------+------------+------+-------+
| Good Mood   | Tom    | PUB Tigger | BJ   |   110 |
| Rainy Day   | Jerry  | PUB Rabbit | SZ   |   230 |
| On the Road | Jerry  | PUB Lion   | SH   |   220 |
+-------------+--------+------------+------+-------+
3 rows in set (0.00 sec)

MariaDB [temp]>


同一个table被查询两次

对上面的场景进行增强,假设一本书有两个作者。

新建一个表book_en。

create table book_en (id int not null auto_increment, name varchar(20) not null, author1 int not null, author2 int not null, pub int not null, price int default 0, primary key (id));

insert into book_en (name, author1, author2, pub, price) values ("A Bad Habit", 1, 2, 1, 110);
insert into book_en (name, author1, author2, pub, price) values ("Keep Healthy", 2, 1, 3, 230);
insert into book_en (name, author1, author2, pub, price) values ("Toast", 1, 2, 2, 220);


查询:

MariaDB [temp]> select * from book_en;
+----+--------------+---------+---------+-----+-------+
| id | name         | author1 | author2 | pub | price |
+----+--------------+---------+---------+-----+-------+
|  4 | A Bad Habit  |       1 |       2 |   1 |   110 |
|  5 | Keep Healthy |       2 |       1 |   3 |   230 |
|  6 | Toast        |       1 |       2 |   2 |   220 |
+----+--------------+---------+---------+-----+-------+
3 rows in set (0.00 sec)

MariaDB [temp]> select * from author;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | Tom   |   20 |
|  2 | Jerry |   25 |
+----+-------+------+
2 rows in set (0.00 sec)

MariaDB [temp]> select book_en.name as name, a1.name as author1, a2.name as author2, publish.name as publish, publish.city as city, book_en.price from book_en, author a1, author a2, publish where book_en.author1=a1.id and book_en.author2=a2.id and book_en.pub=publish.id;
+--------------+---------+---------+------------+------+-------+
| name         | author1 | author2 | publish    | city | price |
+--------------+---------+---------+------------+------+-------+
| Keep Healthy | Jerry   | Tom     | PUB Rabbit | SZ   |   230 |
| A Bad Habit  | Tom     | Jerry   | PUB Tigger | BJ   |   110 |
| Toast        | Tom     | Jerry   | PUB Lion   | SH   |   220 |
+--------------+---------+---------+------------+------+-------+
3 rows in set (0.00 sec)

MariaDB [temp]> select book_en.name as name, a1.name as author1, a2.name as author2, publish.name as publish, publish.city as city, book_en.price from book_en inner join author a1 inner join author a2 inner join publish where book_en.author1=a1.id and book_en.author2=a2.id and book_en.pub=publish.id;
+--------------+---------+---------+------------+------+-------+
| name         | author1 | author2 | publish    | city | price |
+--------------+---------+---------+------------+------+-------+
| Keep Healthy | Jerry   | Tom     | PUB Rabbit | SZ   |   230 |
| A Bad Habit  | Tom     | Jerry   | PUB Tigger | BJ   |   110 |
| Toast        | Tom     | Jerry   | PUB Lion   | SH   |   220 |
+--------------+---------+---------+------------+------+-------+
3 rows in set (0.00 sec)

MariaDB [temp]>


上面的where可以改成on。

另外一个例子

城市city,两个城市之间的距离distance。

建表&准备测试数据

create table city (id int not null auto_increment, name varchar(20), primary key (id));
create table distance (id int not null auto_increment, from_id int not null, to_id int not null, dist int not null, primary key (id));

insert into city (name) values("BJ");
insert into city (name) values("SH");
insert into city (name) values("SZ");

insert into distance (from_id, to_id, dist) values (1, 2, 1200);
insert into distance (from_id, to_id, dist) values (2, 3, 2300);
insert into distance (from_id, to_id, dist) values (3, 1, 3100);


数据验证

MariaDB [temp]> select * from distance;
+----+---------+-------+------+
| id | from_id | to_id | dist |
+----+---------+-------+------+
|  1 |       1 |     2 | 1200 |
|  2 |       2 |     3 | 2300 |
|  3 |       3 |     1 | 3100 |
+----+---------+-------+------+
3 rows in set (0.00 sec)

MariaDB [temp]> select * from city;
+----+------+
| id | name |
+----+------+
|  1 | BJ   |
|  2 | SH   |
|  3 | SZ   |
+----+------+
3 rows in set (0.00 sec)


Join

MariaDB [temp]> select c1.name as city1, c2.name as city2, distance.dist dist from distance inner join city c1 inner join city c2 where distance.from_id=c1.id and distance.to_id=c2.id;
+-------+-------+------+
| city1 | city2 | dist |
+-------+-------+------+
| BJ    | SH    | 1200 |
| SH    | SZ    | 2300 |
| SZ    | BJ    | 3100 |
+-------+-------+------+
3 rows in set (0.01 sec)

MariaDB [temp]> select c1.name as city1, c2.name as city2, distance.dist dist from city c1 inner join city c2 inner join distance on distance.from_id=c1.id and distance.to_id=c2.id;
+-------+-------+------+
| city1 | city2 | dist |
+-------+-------+------+
| BJ    | SH    | 1200 |
| SH    | SZ    | 2300 |
| SZ    | BJ    | 3100 |
+-------+-------+------+
3 rows in set (0.00 sec)

MariaDB [temp]>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MySql Join