Mysql multi-table join query example explanation

Let's take a look at two tables:

 the student table:

article Table:

A: multi-table joins Type:

(1) is connected:

Can CROSS JOIN In MySQL or is omitted, i.e. CROSS JOIN, or use ',' 

SELECT * FROM table1 CROSS JOIN table2 
SELECT * FROM table1 JOIN table2 
SELECT * FROM table1,table2

(2) connecting the left and right connections =:

SELECT article.id,student.name FROM article LEFT JOIN student ON article.id=student.id

II: Table constraints:

(1) where conditions:

mysql>SELECT * FROM table1,table2 WHERE table1.id=table2.id;

 

(2) on:

SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id;

(3) basic join usage:

left join (left coupling) returns the left includes all records in the table and the right table record is equal to the coupling field 

right join (the right coupling) Returns includes all the records in the table and the right left table records equal field coupling

inner join (equivalent connections) returns only two tables are equal field coupling line

First, we assume there are two tables A and B, and their fields are the table structure:

Table A:

    ID Name

    1 Tim

     2 Jimmy

     3 John

     4 Tom

Table B:

    ID Hobby

    1 Football

    2 Basketball

    2 Tennis

   4 Soccer

 

(3.1) the connection:

SELECT A.name,B.`hobby` FROM A,B WHERE A.`id`=B.`id`

(2) left connection:

SELECT A.Name FROM A LEFT JOIN B ON A.id = B.id

(3) joint inquiry:

SELECT t1.id,t2.id,t3.id FROM t1,( t2 LEFT JOIN t3 ON (t3.id=t1.id) ) WHERE t1.id=t2.id;

 

 

 

 

 

 

 

 

###################################episode############## ###############################

to sum up:

When we associate a table JOIN operation, and back to ON WHERE condition is not clear we have not noticed, there is no difference, some friends may think the conditions which follow them are the same, you can talk to in the back oN, if desired, can also be followed WHERE.

For some this case is concerned, it may be correct, but the right kind of purely good luck to hit. For JOIN behind with the conditions, they have exactly what kind of difference it back in the ON and WHERE?

Reply:

For the operation of the associated table JOIN participation, if required connection does not meet the conditions of the line is also within the scope of our inquiry, we would put the necessary conditions for connection on the back ON, and not on the back WHERE, if we put the join condition in the back wHERE, then all the LEFT, RIGHT, and so these operations has no effect, in this case, its effect is exactly the same INNER connection. For those conditions do not affect the selected row, on or behind ON WHERE can.

Remember: All connections conditions will need to be placed back ON, otherwise all of the foregoing LEFT, RIGHT and associate as furnishings, while no effect.

#####################################episode############ ##################################

Also need to pay attention to:

When it comes to multi-table queries in MySQL, required under the circumstances inquiry, like a good use of a higher kind of connection efficiency.

 

 

 

 

 

 

 

 

 

 

Published 56 original articles · won praise 29 · views 20000 +

Guess you like

Origin blog.csdn.net/knight_zhou/article/details/103733555