如果是左连接的情况下对右表进行限
select wr.id,wr.merchant_id,wm.merchant_id,wm.id,wr.wechat_id,wm.wechat_id,wm.belong from wechat_report wr left join (select * from wechat_merchant where belong = 3) wm on wr.wechat_id = wm.wechat_id order by wm.wechat_id; select wr.id,wr.merchant_id,wm.merchant_id,wm.id,wr.wechat_id,wm.wechat_id,wm.belong from wechat_report wr left join wechat_merchant wm on wr.wechat_id = wm.wechat_id where wm.belong = 3 order by wm.wechat_id
查询出来的结果:
结果完全不一样,我想,这是因为第一条sql语句提前对右表限制了数据,而对左表其实就是主表的数据没有影响。相当于在建立临时表里面的on,虽然结果一样,效率却大相庭径。
[SQL] select wr.id,wr.merchant_id,wm.merchant_id,wm.id,wr.wechat_id,wm.wechat_id,wm.belong from wechat_report wr right join wechat_merchant wm on wr.wechat_id = wm.wechat_id and wm.belong = 3 order by wm.wechat_id; 受影响的行: 0 时间: 78.559s [SQL] select wr.id,wr.merchant_id,wm.merchant_id,wm.id,wr.wechat_id,wm.wechat_id,wm.belong from wechat_report wr right join wechat_merchant wm on wr.wechat_id = wm.wechat_id where wm.belong = 3 order by wm.wechat_id; 受影响的行: 0 时间: 5.681s
跟我想的不一样了。。。太尴尬了,结果不同了,是这些数据多了
然后再稍微修改一下子。
select wr.id,wr.merchant_id,wm.merchant_id,wm.id,wr.wechat_id,wm.wechat_id,wm.belong from wechat_report wr right join (select * from wechat_merchant where belong = 3) wm on wr.wechat_id = wm.wechat_id order by wm.wechat_id;
得出结果:
看来on后面的条件不能随便加。。。。
原文:https://www.cnblogs.com/learnerlx/p/14043045.html