SQL语法——Join详解

时间:2023-03-10 02:07:58
SQL语法——Join详解

一、INNER JOIN

SQL语法——Join详解

用法:

select column_name(s)
from table 1
INNER JOIN table 2
ON
table 1.column_name=table 2.column_name

例子:

 两个表:three,user

SQL语法——Join详解

select* from three inner join user;

SQL语法——Join详解

select* from three inner join user on three.id = user.id;

SQL语法——Join详解

二、LEFT JOIN

SQL语法——Join详解

 用法:

select column_name(s)
from table 1
LEFT JOIN table 2
ON table 1.column_name=table 2.column_name

例子:

SQL语法——Join详解

select * from three left join user on three.id=user.id;

SQL语法——Join详解

三、RIGHT JOIN

SQL语法——Join详解

 用法:

select column_name(s)
from table 1
RIGHT JOIN table 2
ON table 1.column_name=table 2.column_name

例子:

SQL语法——Join详解

select * from three right join user on three.id=user.id;

SQL语法——Join详解

四、FULL OUTER JOIN

SQL语法——Join详解

用法:

select column_name(s)
from table 1
FULL OUTER JOIN table 2
ON table 1.column_name=table 2.column_name

例子:

select * from three full outer join user on three.id=user.id;

SQL语法——Join详解

mysql 报错不支持full join ,但是可以用下面的这种写法取代:

select * from three left outer join user on three.id=user.id union select * from three right outer join user on three.id=user.id;

SQL语法——Join详解

参考资料:

https://www.cnblogs.com/reaptomorrow-flydream/p/8145610.html