SQL语句优化 in 改写成join 、1对多取出1对1、笛卡尔积、 where is null 、NULL导致not in写法不对、行号问题


x.a必须是唯一的,才能改写成join的语句。

select * from x where a in (select a from y );

select  distinct x.* from x join y  on x.a=y.a;

已经能满足大多数场景。

二、一对多

only_full_group_by

研发提供的错误写法

   SELECT
Openid,
max(OperateTime),
      SourceIndex
FROM
A
GROUP BY
Openid;

由于group by 中只有Openid,sql_mode如果严格,执行是不成功的。

正确语句如下:

 

select * from A  where (Openid,OperateTime) in (select Openid,max(OperateTime) from  A  group by Openid);

########################################################################################

两张表1对多,取出1对1的信息

 

 

where lie is null  ,不要用and lie is null

###########################################

笛卡尔积:

select * from a,b; 没有条件

########################################

 

 

 

为什么列里不让有NULL,NULL会导致查询not in 查出来的数据有问题。

正确的写法:

select * from a where id not in (select id from b where id is not null );

######################################################################

 行号:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM