我有 2 个具有一对多关系的表。表 A 有列
- id
- Name
表 B 与表 A 关联。表 B 属于表 A。表 B 具有列
- id
- TableA_id_ForeignKey
- TimeChange
- Status
我想编写一个 MySQL 查询,以便当表 B 没有表 A 的关联行条目时,虚拟数据将打印在表 A 中该特定行的输出中。结果将类似于;
TableA_Name 0000-00-00 00
人们将能够辨别表 A 中的哪一行没有与某些虚拟数据关联的行。非常感谢你的帮助。
最佳答案
select
b.*,
coalesce(a.name, '0000-00-00 00') as dummy
from
b
left join a on b.a_fk = a.id
通过此左连接
,您可以获取b
中的所有行,无论a
中是否存在匹配行。如果没有匹配的行,则返回null
。然后,coalesce()
函数返回其第一个参数,该参数不为 null
。
更新:理解问题错误......
select
a.*,
'hello' as dummy /*with the where clause you just get rows where there's no match anyway*//
from
b
right join a on b.a_fk = a.id
where b.id is null
关于mysql - 检测关联表中的空行并打印出虚拟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219184/