Mysql connection query (left connection, right connection, internal connection), mysql Query

Source: Internet
Author: User
Tags mysql query

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.