SQL- associated with the query [turn]

T_A A Table T_B B standard, id `field of the table is associated with the table to
create the associated table structure

CREATE TABLE Table_B( id INT(2), serNum VARCHAR(10) ); CREATE TABLE Table_A( id INT(2), serNum VARCHAR(10) ); INSERT INTO table_a (id, serNum) VALUES (1,'A000101'),(2,'A000102'),(3,'A000103'),(5,'A000104'),(8,'A000105'),(4,'A000106'); INSERT INTO table_b (id, serNum) VALUES (1,'B000201'),(2,'B000202'),(3,'B000203'),(6,'B000204'),(7,'B000205'),(9,'B000206'); 

 

Table_A
id serNum
------ ---------
1 A000100
2 A000102
3 A000103
5 A000104
8 A000105
4 A000106

Table_B
id serNum
------ ---------
1 B000201
2 B000202
3 B000203
6 B000204
7 B000205
9 B000206

1. inner join join queries within

SELECT a.*,b.*
FROM table_a a
INNER JOIN table_b b
ON a.id=b.id

Query results:
the above mentioned id serNum the above mentioned id serNum
------ ------- ------ ---------
1 A000100 B000201 1
2 2 A000102 B000202
3 A000103 3 B000203

2. left join left-associative queries

The results left the table to table to the association as a basis for the right table, the query is a subset of the left table

SELECT a.*,b.*
FROM table_a a
LEFT JOIN table_b b
ON a.id=b.id

Results:
ID serNum ID serNum
------ ------- ------ ---------
. 1 A000100 B000201. 1
2 2 B000202 A000102
. 3 A000103. 3 B000203
. 5 A000104 ( NULL) (NULL)
. 8 A000105 (NULL) (NULL)
. 4 A000106 (NULL) (NULL)

3.right join right-associative queries

Results as a basis for the right table to table associated with the left table, the query is a subset of the right table

SELECT a.*,b.*
FROM table_a a
RIGHT JOIN table_b b
ON a.id=b.id

Results:
ID serNum ID serNum
------ ------- ------ ---------
. 1 A000100 B000201. 1
2 2 B000202 A000102
. 3 A000103. 3 B000203
(NULL) (NULL). 6 B000204
(NULL) (NULL). 7 B000205
(NULL) (NULL). 9 B000206

4. Left connection - the connection

Take the left part of the table set, but not the presence of the right table

SELECT a.*,b.*
FROM table_a a
LEFT JOIN table_b b
ON a.id=b.id WHERE b.id IS NULL

Query results:
the above mentioned id serNum the above mentioned id serNum
------ ------- ------ --------
5 A000104 (NULL) (NULL)
8 A000105 (NULL) (NULL)
4 A000106 (NULL) (NULL)

The right connection - the connection

A portion of the data with a table, but not the presence of the left table

SELECT a.*,b.*
FROM table_a a
RIGHT JOIN table_b b
ON a.id=b.id WHERE a.id IS NULL

Query results:
the above mentioned id serNum the above mentioned id serNum
------ ------ ------ ---------
(NULL) (NULL) 6 B000204
(NULL) (NULL) 7 B000205
(NULL) (NULL) 9 B000206

Reproduced in: https: //www.cnblogs.com/cwind/p/11062440.html

Guess you like

Origin blog.csdn.net/weixin_34191734/article/details/93209062