mysql - 从具有空数据记录的类型表中选择 *
问题描述
Types
ID | Title
1 | blue
2 | red
和
Colours
ID| patientID | date |Type
1 | 1 | 2019 |1
2 | 1 | 2019 |2
3 | 2 | 2018 |1
即使记录的类型不存在,我也需要记录,它应该带回 null。因此,即使只有 1 条记录,患者 2 也应该带回 2 条记录。
Select Title,date from Colours Left Join Types On Colours.Type = Types .ID Where patient.ID = 1
{
patient1:[{
Title: blue
date : 2019
},
{
Title: red,
date : 2019
}]
}
但是,我需要患者 2 恢复所有颜色为空值的日期。目前获得:
Select Title,date from Colours Left Join Types On Colours.Type = Types .ID Where patient.ID = 2
patient:[{
Title: blue
date : 2018
}]
但是我需要:
patient:[{
Title: blue
date : 2018
},
{
Title: red,
date : null
}]
}
解决方案
我想你想要一个left join
:
select t.Title, c.date
from types t left join
Colours c
On c.Type = t.ID and c.patientID = 2;
推荐阅读
- sql - 在计算中调整 CAST 的问题(精度)
- ios - 更新到 Xcode 13.0 后 react-native 崩溃
- postgresql - 如何连接到 host.docker.internal postgres 实例?
- android - 应用程序在 Play 商店崩溃,但不在 Play 管理中心
- go - 如何解决问题 Mark and Toys 关于 Hackerrank 的问题
- gradle - 收到一个带有未知 id、Gradle、Junit4、SystemRules 的测试失败事件
- github - GitHub Actions 部署到 Heroku 失败
- python - 如何提高 Numpy loadtxt 的精度?
- reactjs - ToHaveStyleRule 函数不适用于悬停规则
- javascript - 添加带有价格的商品时如何自动添加和更新总价