MySQL Left Join not returning null values for joined table(MySQL Left Join 不返回连接表的空值)
问题描述
请帮助我执行以下连接两个表(A 和 B)的 MySQL 查询:
Please help me with the following MySQL query, which joins two tables (A and B):
SELECT * from A
left join B on A.sid = B.sid
where (rCode = 1 Or rCode = 2 Or rCode = 3 Or rCode = 5)
AND (rYear = 2011 or rYear is null)
roleCode 是表 A 中的一个字段,rYear 是表 B 中的一个字段
roleCode is a field in table A and rYear is a field in table B
结果集与预期不符.只返回了 185 行,但 A 表中有 629 行符合 where 条件.表 B 中没有匹配行的行不应该返回其 B 字段的空值吗?
The result set is not as expected. Only 185 rows are returned, but there are 629 rows in table A that match the where condition. Shouldn't the rows without a matching row in table B be returned with null values for their B fields?
推荐答案
您不应在 WHERE 子句中指定 rYear.这些限制了您加入后的结果.您应该在 ON 子句中指定 rYear 以从表 B 中取回带有 NULL 的记录.
You should not specify rYear in a WHERE clause. Those limit your results after the join. You should specify rYear in an ON clause to get back records with NULL from table B.
SELECT * from A
left join B
on A.sid = B.sid
AND (rYear = 2011 or rYear is null)
where (rCode = 1 Or rCode = 2 Or rCode = 3 Or rCode = 5)
这篇关于MySQL Left Join 不返回连接表的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL Left Join 不返回连接表的空值
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
