MySQL join 与where的执行顺序

本文详细解析了MySQL中`JOIN`与`WHERE`关键字的执行顺序,比较了不同写法的优劣,并讨论了可能导致错误的用法。重点讲解了先过滤后join和先join后过滤的不同效果,以及左连接和同时使用条件的示例。
摘要由CSDN通过智能技术生成

MySQL join 与where的执行顺序

-- 写法ok 先过滤后join
select * from(select * from u1 where u1.id=3)t join u2  on t.id=u2.id;
id  name id(1) sex
3	c	3	male

-- 写法ok 先join后过滤
select * from u1 join u2 on u1.id=u2.id where u1.id=3;
id  name id(1) sex
3	c	3	male

-- 写法ok
select * from u1 join u2 on u1.id=u2.id and u1.id=3;
id  name id(1) sex
3	c	3	male

-- 此种写法不可以 报错
select * from u1 where u1.id=3 join u2 on u1.id=u2.id;
-- You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near 'join u2 on u1.id=u2.id' at line 1

select * from u1 left join u2 on u1.id=u2.id where u1.id=3;
id  name id(1) sex
3	c	3	male

select * from u1 left join u2 on u1.id=u2.id and u1.id=3;
id  name id(1) sex
3	c	3	male
1	a	null null	
2	b	null null
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值