SQL join two or more tables

When returned to the database record by connecting two or more tables, generates an intermediate temporary table, and then returns this to the user the temporary table.

In use left jion, and where the difference between on the following conditions:

1, on condition that the conditions used in the generation of a temporary table, regardless of whether it is on the condition is true, will return the records to the left of the table.

2, where the condition is good after the temporary table generation, then the temporary table of filter conditions. At this time has no meaning left join in (must return the records left of the table), and the condition is not really all filtered out.


book table:

stu table:

1. the connector

1.1 Equijoins: using the equal sign (=) operator to compare the value of the column is connected in the connection conditions, which query results are listed in all the columns in the connection table, including duplicate columns.

1.2 Unequal connect: Other comparison value of the column other than equal comparison operator is connected in the connection conditions. These operators include>,> =, <,!>, <!>.

1.3 NATURAL connection: the connection conditions using equal (=) operator to compare the value of the column is connected, but it is noted that row select list included in the query result set and remove duplicate columns in the connection table. The connection: the connection with the connection condition listed query matching data row, using a column which is connected to the comparison value comparison operators column.

select * from book as a,stu as b where a.sutid = b.stuid

select * from book as a inner join stu as b on a.sutid = b.stuid

FIG connected as a result, is connected in a.stuid = b.stuid.

2. The external connection

2.1 left join: the reference table is left, the a.stuid = b.stuid data connection, then no left table entry corresponding to the display, the right table column is NULL

select * from book as a left join stu as b on a.sutid = b.stuid

2.2 Right connection: the right based on the reference table, the a.stuid = b.stuid data connection, and then displayed in the corresponding entry of the table does not have the right, the left table column is NULL

select * from book as a right join stu as b on a.sutid = b.stuid

2.3 fully connected: full outer join to return the left table and all rows in the right table. When a line does not match rows in another table, the other select list column contains a null value. If there is a match between the tables, the entire result set row contains data values ​​of the base table.

select * from book as a full outer join stu as b on a.sutid = b.stuid

3. The cross-connect

Cross-connect: Cross joins return all rows from the left table, each row in the left table and the right combination of all the rows in the table. Cross coupled also called Cartesian products.

select * from book as a cross join stu as b order by a.id


It is not, directly (below two joke ha ~)

DROP DATABASE test; or RM -rf *  (delete all files in the current directory, the deleted file, generally can not be restored!)

Delete library on foot, slipped stroll ......

 

Guess you like

Origin blog.csdn.net/dq932073495/article/details/91573873