gpt4 book ai didi

mysql - 如何返回连接表中所有不匹配的行,如果匹配则检查条件?

转载 作者:行者123 更新时间:2023-11-29 14:31:54 24 4
gpt4 key购买 nike

User 时如何返回表的所有行

1. a row has not any match with join table ex **UserDetails**
2. a row has a match in **UserDetails**, check some condition ex: user location is **IND**

示例表

     User
-----------------------
id name
-----------------------
1 Hearaman
2 Ramse
3 Temmy
4 Robert

UserDetails
-----------------------------
id user_id location
1 3 USA
2 4 IND

Expected results
--------------------------
id name location
--------------------------
1 Hearaman null
2 Ramse null
4 Robert IND

最佳答案

您可以使用left join 并进行过滤

select u.id, u.name, ud.location
from User u
left join UserDetails ud on ud.user_id = u.id
where ud.user_id is null or
ud.location = 'IND';

关于mysql - 如何返回连接表中所有不匹配的行,如果匹配则检查条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50489428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com