Mysql join Operation and Mysqljoin operation
Join type
1. Inner join: the records that match the joined fields in the two tables form a join of the record set.
2. Outer Join: outer left join and outer right join.
Background
create table java (name varchar(255));insert into java values ('java1'),('java2'),('blue'); create table mysql (name varchar(255));insert into mysql values ('mysql1'),('mysql2'),('blue');
Inner join
select * from java,mysql where java.name=mysql.name;SELECT * FROM java JOIN mysql ON java.name=mysql.name;SELECT * FROM java INNER JOIN mysql ON java.name=mysql.name;SELECT * FROM java CROSS JOIN mysql ON java.name=mysql.name;SELECT * FROM java STRAIGHT_JOIN mysql ON java.name=mysql.name;
These four statements are all inner joins and the returned results are all
+------+------+| name | name |+------+------+| blue | blue |+------+------+
- Each comma in the table_reference entry is considered as an internal union.
- The default JOIN operation is inner join.
- In syntax, cross join is equivalent to inner join.
- STRAIGHT_JOIN is the same as JOIN. Except for some differences, the left table will be read before the right table. STRAIGH_JOIN can be used to arrange tables in wrong order.
The internal join syntax is as follows:
join_table: table_reference [INNER | CROSS] JOIN table_factor [join_condition] | table_reference STRAIGHT_JOIN table_factor | table_reference STRAIGHT_JOIN table_factor ON condition
Outer Join
Left join
SELECT * FROM java LEFT JOIN mysql ON java.name=mysql.name;
The result is
+-------+------+| name | name |+-------+------+| java1 | NULL || java2 | NULL || blue | blue |+-------+------+
Therefore, we can see from the above results that the names of the java1 and java2 records in the Java table do not have the corresponding names in the MySQL table, so they are empty, however, the java1 and java2 records are still found in all columns of java, and the column of all columns in mysql table is NULL. The remaining blue record is the result of the connection between the java table and the mysql table.
If no matching record exists for the right table in the ON or USING section of left join, a row with all columns set to NULL is used for the right table. If a table does not have a corresponding part in other tables, you can use this method to search for records in this table:
SELECT * FROM java LEFT JOIN mysql ON java.name=mysql.name WHERE mysql.name IS NULL;
This SQL statement is used to identify the persons in java but not in mysql. Here it is obvious that the personnel 'java1' and 'java2' meet the requirements.
Right join
SELECT * FROM java RIGHT JOIN mysql ON java.name=mysql.name;
The returned result is
+------+--------+| name | name |+------+--------+| NULL | mysql1 || NULL | mysql2 || blue | blue |+------+--------+
The result of the right join is similar to that of the left join, but this is the result set that is saved in the mysql table.
External join syntax
join_table:| table_reference LEFT [OUTER] JOIN table_reference join_condition | table_reference NATURAL [LEFT [OUTER]] JOIN table_factor | table_reference RIGHT [OUTER] JOIN table_reference join_condition | table_reference NATURAL [RIGHT [OUTER]] JOIN table_factor
USING (column_list) clause
Used to name a series of columns. These columns must exist in both tables.
SELECT java.*,mysql.* FROM java LEFT JOIN mysql USING (name);
Result returned
+-------+------+| name | name |+-------+------+| java1 | NULL || java2 | NULL || blue | blue |+-------+------+
Join Operation Sequence
SELECT * FROM t1 left join (t2, t3, t4) ON (t2.a = t1.a AND t3. B = t1. B AND t4.c = t1.c ); -- equivalent to SELECT * FROM t1 left join (t2 cross join t3 cross join t4) ON (t2.a = t1.a AND t3. B = t1. B AND t4.c = t1.c)
Influence of parentheses on join Sequence
SELECT t1.id, t2.id, t3.id FROM t1, t2 left join t3 ON (t3.id = t1.id) WHERE t1.id = t2.id; -- in fact, SELECT t1.id, t2.id, t3.id FROM t1, (t2 left join t3 ON (t3.id = t1.id) WHERE t1.id = t2.id; -- SELECT t1.id, t2.id, t3.id FROM (t1, t2) left join t3 ON (t3.id = t1.id) WHERE t1.id = t2.id;
Brackets are very important here, so we should not forget to write more parentheses when writing such a query. At least this can avoid many errors.
The above is the Mysql join Operation introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!