The join statement Mysql

mysql one powerful features: join

# Before group by must be placed in order by and limit, or will be error 
# you can use Mysql in SELECT, UPDATE, and DELETE statements JOIN jointly multi-table queries. 
# The JOIN commonly divided into the following three categories (but not only these three types): 
# INNER JOIN (the connection, or equivalent connection): Get Records matching fields in the relationship between two tables; using MySQL INNER JOIN (may be use omitted INNER JOIN, the same effect) 
# the lEFT the JOIN (left connection): Get the left table all records, even if no record corresponding to the right table matching. 
# RIGHT the JOIN (Right link): a LEFT JOIN contrast, all records in the table for acquiring the right, the left table even if there is no corresponding matching records.

1. First two tables:

the SELECT  *  from my_test_copy; # The first table

 

the SELECT  *  from my_test; # second table

 

2.   Inner the Join , will be joined to form a new column

select a.id, a.name_people, b.name_adress from my_test_copy as a inner join my_test as b on a.name_adress=b.name_adress; #可用,或者用:(即不用 'as' 也行)
select a.id, a.name_people, b.name_adress from my_test_copy a inner join my_test b on a.name_adress=b.name_adress; #也可用

 

SELECT a.id, a.name_people, a.name_adress from my_test A Inner  the Join my_test_copy B on a.name_adress = b.name_adress; # for the corresponding column in the column on the Cartesian product of the required return

 

SELECT  *  from my_test A Inner  the Join my_test_copy B ON a.name_adress = b.name_adress; # same row automatic numerical difference

 

3. The left connecting left join, in any case, will return a value A, b is not present if the value indicates a NULL; represents connections connected together to form a new column, it is also considered Cartesian product <a, b>; left meaning connection lEFT JOIN is the intersection of two tables left table plus the rest of the data. Still speaking from the Cartesian product point of view, it is to start to pick Cartesian product in the ON clause condition holds records, and then add the remaining records from the left table (see the results of the last three).

select a.id, a.name_people, a.name_adress, b.id as b_id, b.name_people as name_people from my_test_copy a left join my_test b on a.name_adress=b.name_adress;

 

select * from my_test_copy a left join my_test b on a.name_adress=b.name_adress where b.name_adress is NULL; # 即left join中左边多余的部分

 

4. 右连接 right join,不管怎样,b的值都会返回,a的值中如果不存在则用NULL表示;连接表示连在一起,形成新的列,也算笛卡尔积 <a, b>;同理右连接RIGHT JOIN就是求两个表的交集外加右表剩下的数据。再次从笛卡尔积的角度描述,右连接就是从笛卡尔积中挑出ON子句条件成立的记录,然后加上右表中剩余的记录。

select a.id, a.name_people, a.name_adress, b.id as b_id, b.name_people as name_people from my_test_copy a right join my_test b on a.name_adress=b.name_adress;

 

select * from my_test_copy a right join my_test b on a.name_adress=b.name_adress where a.name_adress is NULL; # 即right join中左边多余的部分,此时该表为空。

 

5. 使用 using进行连接,using与on的效果相同,但是要两个表的列名相同

# using 
select * from my_test_copy right join my_test using(name_adress);

6. where也可以用于连接

select * from my_test_copy, my_test where my_test_copy.name_adress=my_test.name_adress;  # 这样也可以做到连接两个表

7 附加:外连接,即求两个集合的并集。从笛卡尔积的角度讲就是从笛卡尔积中挑出ON子句条件成立的记录,然后加上左表中剩余的记录,最后加上右表中剩余的记录。另外MySQL不支持OUTER JOIN,但是我们可以对左连接和右连接的结果UNION操作来实现,例如:

select * from my_test_copy a left join my_test b on a.name_adress=b.name_adress
union 
select * from my_test_copy a right join my_test b on a.name_adress=b.name_adress;

 

参考:

https://www.cnblogs.com/beili/p/9140019.html

https://www.runoob.com/w3cnote/sql-join-image-explain.html

https://www.runoob.com/mysql/mysql-join.html

Guess you like

Origin www.cnblogs.com/qi-yuan-008/p/12337587.html