gpt4 book ai didi

MySQL 选择连接空值

转载 作者:行者123 更新时间:2023-11-29 19:06:37 24 4
gpt4 key购买 nike

您好,我正在尝试查询,该查询将显示每 4 个表中的 4 个值。

select i.name, c.name, s.name, d.name
from instructor i
join course c
on i.pid = c.instructor
join course_taken ct
on c.id = ct.cid
join student s
on s.id = ct.sid
join department d
on s.major = d.id or s.major is null
where i.name = 'lee';

除了空部分之外一切都很好。

表结构

--------------------------------------------------
| student | course | instructor | department |
--------------------------------------------------
| name | name | id | id |
--------------------------------------------------
| id | id | name | name |
--------------------------------------------------
| major |instructor| department | |
--------------------------------------------------

结果

--------------------------------------------------
| i.name | c.name | s.name | d.name |
--------------------------------------------------
| kim | math | jack | cs |
--------------------------------------------------
| kim | math | john | cs | --> THIS VALUE IS
--------------------------------------------------
| kim | math | john | ss | --> NULL AND SHOULD BE PRINTED IN NULL
--------------------------------------------------
| kim |math | json | ss |
--------------------------------------------------

如果“john”中的“cs”和“ss”为 NULL,如何打印 null?

最佳答案

使用COALESCE()函数,例如

select coalesce(i.name,'NULL') as iName, ...
from instructor i

根据您的评论,您似乎根本不想从 department 表中输出 name 列。在这种情况下,只需选择 NULL 即可,例如

select i.name, c.name, s.name, null

(或)

select i.name, c.name, s.name, 'null'

关于MySQL 选择连接空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43436140/

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