简体   繁体   English

mysql连接查询左表中的空值

[英]mysql join query on null value in the left table

I have the following tables 我有下表

User table 用户表

user_id   name   location_id  status_id 
1         test   1               1
2         john   NULL            1
3         royi   1               1
4         mahi   2               1

Location table 位置表

location_id   location_name   location_type  status_id 
1            US                  1               1
2            UK                  1               1
3            BR                  1               1
4            IN                  2               1

I want to join the user and location table with location_id and find the null record also. 我想将用户和位置表与location_id联接在一起,并找到空记录。 I have implemented the following query but it won't working. 我已经实现了以下查询,但无法正常工作。

SELECT * 
FROM user u 
    left join location l 
        ON u.location_id = l.location_id 
WHERE 
    u.status_id = 1 
    and l.status_id = 1 
    and l.location_type = 1 
    or u.location_id IS NULL

I am expecting the result like 我期望结果像

user_id   name   location_id  status_id location_id   location_name   location_type status_id 
1         test   1               1        1            US                  1               1
2         john   NULL            1        NULL          NULL              NULL             NULL  
3         royi   1               1        1            US                  1               1
4         mahi   2               1        2            UK                  1               1

When i change the l.location_type to 2 , the expected result like follows 当我将l.location_type更改为2时,预期结果如下

user_id   name   location_id  status_id location_id   location_name   location_type status_id 
2         john   NULL            1        NULL          NULL              NULL             NULL 

Try this : 尝试这个 :

SELECT *
FROM user u 
    left join location l 
        ON u.location_id = l.location_id 
        and l.status_id = 1 
        and l.location_type = 1 
where 
    u.status_id = 1 

Here is the demo 这是演示

You don't need to use u.location_id IS NULL because left join does not exclude users, it just adds on matched location the location of the user. 您不需要使用u.location_id IS NULL因为左u.location_id IS NULL不排除用户,它只是在匹配的位置添加了用户的位置。

I have seen that you edited your post and changed the status_id of john from 2 to 1. Of course if John status id is 2, the query could never retrieve him (remove where u.status_id = 1 if needed) 我已经看到您编辑了帖子,并将john的status_id从2更改为1。当然,如果John的id为2,则查询将永远无法检索到他(如果需要,请删除where u.status_id = 1

Leave me a comment, if the expected output is wrong I'll fix it fastly. 让我发表评论,如果预期的输出错误,我会尽快修复。

I think it's because of this condition u.status_id , I guess you want to write u.status .. 我认为是因为存在这种情况u.status_id ,我想您想写u.status ..

Everything else seems right.. ^^ 其他一切似乎都正确.. ^^

Since in the null records of location_id, the l.status_id and l.location_type will also be null there will be no output with your query. 由于在location_id的空记录中,l.status_id和l.location_type也将为null,因此查询将不会有输出。

You should have. 你应该有。

SELECT * FROM user u 
left join location l ON u.location_id = l.location_id 
WHERE u.status_id = 1 
and ((l.status_id = 1 and l.location_type = 1) or u.location_id IS NULL)

Only when you put them in brackets the OR clause will act properly and show either one of them. 仅当将它们放在方括号中时,OR子句才能正常运行并显示其中一个。

I suggest this should be your query. 我建议这应该是您的查询。

试试这个

SELECT * FROM user u left join location l ON u.location_id = l.location_id 

As you are using left join means you want all records from user and corresponding records from location table along with not exist records in location with null values, you can try below query: 当您使用左联接时,意味着您希望来自用户的所有记录和位置表中的相应记录,以及不存在具有空值的位置中的记录,可以尝试以下查询:

SELECT u.*,l.* 
FROM user u 
left join location l 
ON u.location_id = l.location_id 
WHERE u.status_id = 1 and l.status_id = 1 and (l.location_type = 1 or u.location_id is null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM