MySQL multi-table queries (Cartesian product principle)

  1. First determine which table data to use.
  2. Multiple tables into a first table by Cartesian product.
  3. Then removed illogical data (two removed from the relationship tables).
  4. Finally, as it is a virtual table to add the same condition can be.

Note: The column name is best to use aliases to distinguish table.

 

Cartesian Product

 

 

 

 

Demo:

 

Left and right connection, the inner and outer connection

l              the connection:

Important: it returns all matching records.

  1. 2.          SELECT * from A, B = AX BX WHERE // the connection //

l              outside is connected to the left and right connection connecting the two.

Important: returns all matching records each row applied to the main table as a record key value is null. All secondary table as a null value.

  1. * A left from the Join the SELECT ON AX = b the Order by AX BX   // or called a left outer join left join
  2. * A right from the Join SELECT ON B = AX AX BX Order by   // or right outer connecting said right connection

select clause order

Clause

Explanation

Do you have to use

select

To return the column or expression

Yes

form

Retrieved from table data

Only used when selecting from the table

where

Row-level filtering

no

group by

Packet Description

Used in calculating the aggregate only by group

having

Group stage filter

no

order by

Output Sort Order

no

limit

The number of rows to be retrieved

no

Guess you like

Origin www.cnblogs.com/xiaozengzeng/p/12078158.html