Mysql connection query (left connection, right connection, internal connection), mysql Query
I. mysql common connections
- Inner join (internal JOIN, or equivalent JOIN): obtains records of field matching relationships in two tables.
- Left join: obtains all records in the LEFT table even if no matching records exist in the right table.
- Right join: opposite to left join, it is used to obtain all records in the RIGHT table even if there is no matching record in the LEFT table.
Mysql> select * from name_address; + ---------- + ------ + ---- + | address | name | id | + ---------- + ------ + ---- + | Northwest one road | Zhang San | 1 | northwest second road | Li Si | 2 | northwest Third Road | Wang Wu | 3 | + ---------- + ------ + ---- + 3 rows in setmysql> select * from name_age; + ----- + -------- + ---- + | age | name | id | + ----- + -------- + ---- + | 18 | Zhang San | 1 | 20 | Wang Wu | 2 | 21 | Lu renjia | 3 | + ----- + -------- + ---- + 3 rows in set
1. INNER JOIN
The inner join statement is the same as that of a General connected Table query, that is, the query method is separated by commas.
Mysql> SELECT. 'name',. age, B. address FROM name_age a inner join name_address B WHERE (on). 'name' = B. 'name '; + ------ + ----- + ---------- + | name | age | address | + ------ + ----- + ---------- + | Zhang San | 18 | Northwest one road | Wang Wu | 20 | northwest three roads | + ------ + ----- + ---------- + 2 rows in set
2. LEFT JOIN
Take the data table on the left as the standard
Mysql> SELECT. 'name',. age, B. address FROM name_age a left JOIN name_address B on. 'name' = B. 'name '; + -------- + ----- + ---------- + | name | age | address | + -------- + ----- + ---------- + | Zhang San | 18 | Northwest one road | Wang Wu | 20 | northwest Third Road | Lu renjia | 21 | NULL | + -------- + ----- + ---------- + 3 rows in set
3. RIGHT JOIN
Opposite to left join, that is, the data on the right prevails.
Mysql> SELECT B. 'name',. age, B. address FROM name_age a right JOIN name_address B on. 'name' = B. 'name '; + ------ + ---------- + | name | age | address | + ------ + ---------- + | Zhang San | 18 | Northwest one road | Wang Wu | 20 | northwest three roads | Li Si | NULL | northwest road | + ------ + ---------- + 3 rows in set
The above is the information for MySQL connection query. If you have any questions, please leave a message to discuss and make progress together. Thank you for reading this article and hope to help you. Thank you for your support for this site!