MYSQL Left Join 如何选择 NULL 值?

MYSQL Left Join how do I select NULL values?(MYSQL Left Join 如何选择 NULL 值?)
本文介绍了MYSQL Left Join 如何选择 NULL 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个关于 MySQL 中表连接的问题的后续问题

This is a follow up question to my last question about table joins in MySQL

我需要能够从左连接表中选择 NULL 值.

I need to be able to select the NULL values from the left joined table.

这是我的加入:

table1.id | table1.name | table2.id | table2.surname
        1 |        John |         1 |            Doe
        2 |     Michael |         2 |       Anderson
        3 |        Anna |      NULL |           NULL
        4 |         Sue |      NULL |           NULL

我想select WHERE table2.surname = NULL,但是这样的查询不起作用:

I would want to select WHERE table2.surname = NULL, but a query like this doesn't work:

SELECT table1.*,table2.*
FROM table1
LEFT JOIN table2
    ON table1.id=table2.id
WHERE table2.surname=NULL

我可以稍微理解它背后的逻辑,没有给我任何结果,但必须有一种方法来获取它们的结果?

I can somewhat understand the logic behind it not giving me any results, but there must be a way to grab them results?

感谢任何帮助.

推荐答案

要比较 NULL 值,您必须使用 IS NULL 谓词,如下所示:

To compare NULL values you have to use the IS NULL predicate, like this:

SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.surname IS NULL

这篇关于MYSQL Left Join 如何选择 NULL 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)