[Python] sql-inner join, left join, right join, union

Inner join:
mysql> select * from book_wangjing as book_1 inner join user_wangjing as user_1 on book_1.id=user_1.id limit 2;
+----+------------------------+-------------+-------+--------------+----+---------------------+------------+---------------------+----------+
| id | book_name              | book_author | price | publish_date | id | date1               | date2      | date3               | time     |
+----+------------------------+-------------+-------+--------------+----+---------------------+------------+---------------------+----------+
| 1 | Webdriver Framework Practical Guide | Mr. Wu | 100 | 2018-05-05 | 1 | 2018-05-05 00:08:24 | 2018-05-05 | :08:24|
| 2 | how google test | Mr. Li | 99 | 2018-05-05 | 2 | 2018-05-05 00:08:54 | 2018-05-05 | 08:54 |
+----+------------------------+-------------+-------+--------------+----+---------------------+------------+---------------------+----------+
2 rows in set (0.00 sec)
 
Left join:
mysql> select a.* from author_wangjing as a left join book_wangjing as b on a.author_name=b.book_author;
+----+-------------+--------+
| id | author_name | salary |
+----+-------------+--------+
| 1 | Mr. Wu | 10000 |
|  3 | Mr Jackson  |  15000 |
| 2 | Mr. Zhang | 13000 |
+----+-------------+--------+
3 rows in set (0.07 sec)
 
Right join:
mysql> select * from user_wangjing as user_1 right join book_wangjing as book_1 on user_1.id=book_1.id;
+------+---------------------+------------+---------------------+----------+----+------------------------+-------------+-------+--------------+
| id   | date1               | date2      | date3               | time     | id | book_name              | book_author | price | publish_date |
+------+---------------------+------------+---------------------+----------+----+------------------------+-------------+-------+--------------+
| 1 | 2018-05-05 00:08:24 | 2018-05-05 | 2018-05-05 00:08:24 | 00:08:24 | 1 | Webdriver Framework Practical Guide | Mr. Wu | 100 | 2018 -05-05 |
| 2 | 2018-05-05 00:08:54 | 2018-05-05 | 2018-05-05 00:08:54 | 00:08:54 | 2 | how google test | Mr. Li | 99 | 2018- 05-05 |
| 3 | 2018-05-05 00:08:55 | 2018-05-05 | 2018-05-05 00:08:55 | 00:08:55 | 3 | unit test | Mr. Li | 1 | 2018-05 -05 |
| 4 | 2018-05-05 00:08:56 | 2018-05-05 | 2018-05-05 00:08:56 | 00:08:56 | 4 | function test | Mr. Li | 10 | 2017-11 -10 |
| NULL | NULL                | NULL       | NULL                | NULL     |  5 | function test          | Mr Jackson  |    10 | 2013-06-10   |
+------+---------------------+------------+---------------------+----------+----+------------------------+-------------+-------+--------------+
5 rows in set (0.07 sec)
 
mysql> select a.* from author_wangjing as a right join book_wangjing as b on a.author_name=b.book_author;
+------+-------------+--------+
| id   | author_name | salary |
+------+-------------+--------+
| 1 | Mr. Wu | 10000 |
|    3 | Mr Jackson  |  15000 |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
+------+-------------+--------+
 
union:
mysql> select book_author from book_wangjing union select author_name from author_wangjing;
+-------------+
| book_author |
+-------------+
| Teacher Wu |
| Mr. Li |
| Mr Jackson  |
| Teacher Zhang |
+-------------+
4 rows in set (0.06 sec)
 

1. Inner join: a.author_name and b.book_author appearing in the result set must exist in both tables at the same time

2. Left join: a.author_name in the result set must exist in the left table (author_wangjing table) and be displayed in all

3. Right join: b.book_author that appears in the result set must exist in the right table (book_wangjing table) and display all

4. union: for merging, when using union, the data columns of the left table should be as many as the data columns of the right table

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325315837&siteId=291194637