join查询要点整合

xiaoxiao2021-02-27  605

首先我们要清楚join查询分为五种内查询,左查询,右查询,联合查询,交叉查询。

内查询:select column_name(s)from table1 inner join table2 on  table1.column=table2.column,查询的结果相当于我们集合运算中的交集

左查询和右查询的相当于集合运算中左边的集合或者右边的集合,如果只要左边集合的数据只要设置一个where条件右边集合相等字段设为null同理右边的也是。

联合查询在mysql中并不支持,实现的话就是左查询union all 右查询把两个集合都包括

交叉查询(笛卡尔积) select column from table1 cross join table2

使用join来进行自身表的更新 update table1 join(select table2.username from table1 inner join table2 on table1.username=table2.username)table2 on table1.username=table2.username set over='齐天大圣';

使用join 优化子查询 一般的子查询select table1.username ,table1.over,(select over from table2 where table1.username=table2.username) as over2 from table1

用join优化的子查询select table1.username,table1.over,table2.over from table1 left join table2 on table1.username=table2.username 

转载请注明原文地址: https://www.6miu.com/read-76.html

最新回复(0)