sql join语句总结
所有的join语句都跟连接顺序相关
1、cross join(交叉联接):生成笛卡尔积后,一定不能写on或where条件。
eg:select * from table_2 a cross join table_1 b
2、inner join(内联接):生成笛卡尔积后根据on条件筛选
eg:select * from table_1 a inner join table_2 b on a.id=b.userid
此语句相当于:select * from table_1 a,table_2 b where a.id=b.userid
3、left [outer] join(左外联):在inner join的数据的基础上加上左表中没有满足筛选的记录,并将其剩余字段设为NULL
eg: select * from table_1 a left join table_2 b on a.id=b.userid
4、right [outer] join(右外联):与左外联相似,在inner join的数据的基础上加上右表中没有满足筛选的记录,并将其剩余字段设为NULL
eg:select * from table_1 a right join table_2 b on a.id=b.userid
5、full [outer] join(全外联):(左外联+右外联的超集)
select * from table_1 a full join table_2 b on a.id=b.userid
注:中括号[]表示可省略。