MySQL左外连接where条件包含多表写法
来源:互联网 发布:servlet获取表单数据id 编辑:程序博客网 时间:2024/04/28 14:19
首先创建两个表
CREATE TABLE `student` (`name` VARCHAR (10) DEFAULT NULL,`class` INT (11) DEFAULT NULL)
CREATE TABLE `score` ( `name` varchar(10) DEFAULT NULL, `subject` varchar(10) DEFAULT NULL, `scores` int(11) DEFAULT NULL)
然后分别插入数据
insert into student values('Tom',1);insert into student values('Jerry', 2);insert into score values('Tom','Cat',100);insert into score values('Tom','Cat',99);insert into score values('Jerry','Mouse',80);
Sql1:
SELECTa.`name`,a.`class`,b.`subject`,b.`scores`FROMstudent aLEFT OUTER JOIN score b ON a.`name` = b.`name`WHERE(a.class in (1,2) and b.`subject` = 'Mouse')
返回的数据并不是left join,反而是inner join的数据
+-------+-------+---------+--------+| name | class | subject | scores |+-------+-------+---------+--------+| Jerry | 2 | Mouse | 80 |+-------+-------+---------+--------+
Sql2:
SELECTa.`name`,a.`class`,b.`subject`,b.`scores`FROMstudent aLEFT OUTER JOIN score b ON a.`name` = b.`name` and b.`subject` = 'Mouse'WHERE(a.class in (1,2) )
这个数据是正常的
+-------+-------+---------+--------+| name | class | subject | scores |+-------+-------+---------+--------+| Jerry | 2 | Mouse | 80 || Tom | 1 | NULL | NULL || Tom | 1 | NULL | NULL |+-------+-------+---------+--------+
由此可以看出来,当左外链接的时候,如果where里面包含右表的字段,那么左连接就会转变成内连接。这个应该是where的优先级最高导致的。如果真的需要过滤右表的字段,那么把条件写到on后面就可以了
0 0
- MySQL左外连接where条件包含多表写法
- 左外连接 where条件 on条件
- 浅谈左连接 on和where条件的区别
- MySQL搜索: WHERE 多条件
- MySQL搜索: WHERE 多条件
- where 条件的另类写法
- oracle 左连接写法
- mySQL进行多表的左外连接、右外连接、内连接、全外连接
- MySQL:WHERE后跟多个条件
- mysql单表查询Where 条件查询
- 使用左、右、全、内连接及使用where条件语句的区别
- 使用左、右、全、内连接及使用where条件语句的区别
- SQL:外连接on条件与where条件的区别
- SQL:外连接on条件与where条件的区别
- 浅析Mysql Where条件
- mysql 之 多表查询之 左外连接 内连接 右外连接
- MySQL-99语法:外连接与内连接-(左内连接多表和查询)
- mysql中左连接,右连接,内连接查询,以及与where之间关系
- 51node-1354 选数字
- windows下常用的cmd命令
- android studio 代码混淆打包错误 The same input jar is specified twice
- HDU 4521(小明系列问题——小明序列)
- mysql查看事务和锁情况
- MySQL左外连接where条件包含多表写法
- 八月十一日学习
- VS上写helloworld
- Rails Asset Organization
- light oj 1213 - Fantasy of a Summation (数学计数)
- Tyvj 1008 传球游戏
- 排序算法(二):选择排序法
- VC编程操作word2010生成表格
- zynq-7000学习笔记(二)——编译uboot