mysql left join 可以不写on吗

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql left join 可以不写on吗相关的知识,希望对你有一定的参考价值。

mysq不能嵌套select的子查询。所以我把子查询建成了视图b,主查询通过left join on关联视图b ,形成视图a。
由于视图b中也有left join , 通过left join 关联表c 。通过c确定视图b中的字段state。如果c中有数据,则state=1,否则为0.
然后,在视图a中,取state的值,计算结果。
参考技术A

on 后面没有约束条件,可以写为 on 1 = 1

SELECT
*
FROM
t_user u
LEFT JOIN (SELECT 1, 2, 3 FROM DUAL) t ON 1 = 1

参考技术B 不能 (inner)join可以不加

Mysql Left Join Where On

  



select t1.id,t2.id
from t1
left join t2 on t1.id = t2.id and t1.id>1 and t2.id<>3
在mysql的left join中条件放在on后面和在where后面是不同的;

1. on后面只针对于t2表进行过滤,所以上面的t1.id>1将不起作用,切记,切记;

2. where后面会对最终结果产生影响,所以如果t2.id<>3放到on后面和where后面也是会返回不同的结果;

例如下面脚本一会比脚本二多返回一些数据。

select * from test2
left join test1 on test2.id = test1.id and test1.id<>3
where test2.id <>6;
select * from test2
left join test1 on test2.id = test1.id
where test2.id <>6 and test1.id<>3;

以上是关于mysql left join 可以不写on吗的主要内容,如果未能解决你的问题,请参考以下文章

mysql SELECT LEFT JOIN ON 查询总是超时,大侠能帮忙优化一下吗?

mysql的inner join,left jion,right join,cross join以及on和where的区别

mysql关联left join条件on和where条件的区别及结合coalesce函数

left join 需要distinct吗

MySql 之 left join 查询结果

Mysql Left Join Where On