mysql SQL选择结果为空的值

9wbgstp7  于 5个月前  发布在  Mysql
关注(0)|答案(2)|浏览(41)

如何在SQL中选择结果为空的值例如,我们有下一个表
| 用户|代码|
| --|--|
| user1|代码1|
| user3|代码2|
| 用户4|代码4|
选择select * from table where user in ( 'user1', 'user2', 'user3', 'user4')
选择的结果将是下一个
| 用户|代码|
| --|--|
| user1|代码1|
| user2||
| user3|代码2|
| 用户4|代码4|
如何在SQL中选择空值

iq3niunx

iq3niunx1#

你可以通过将你的条件作为子查询传递,然后将left join应用到你的表中:

select s.user, t.code
from (
  select 'user1' as user union all 
  select 'user2' union all 
  select 'user3' union all 
  select 'user4' 
) as s
left join mytable t on s.user = t.user;

字符串
结果如下:
| 用户|代码|
| --|--|
| user1|代码1|
| user2| null|
| user3|代码2|
| 用户4|代码4|
Demo here

66bbxpm5

66bbxpm52#

可以直接在where子句中过滤

select user,code
from (
  select 'user1' as user, 'abc' as code union all 
  select 'user2','def' union all 
  select 'user3', 'ghi' union all 
  select 'user4' ,NULL
) as s
where code is null;

字符串

相关问题