sql - 同一表上的多个连接,如果连接字段为 NULL,则不返回结果
问题描述
SELECT organizations_organization.code as organization,
core_user.email as Created_By,
assinees.email as Assigned_To,
from tickets_ticket
JOIN organizations_organization on tickets_ticket.organization_id = organizations_organization.id
JOIN core_user on tickets_ticket.created_by_id = core_user.id
Left JOIN core_user as assinees on assinees.id = tickets_ticket.currently_assigned_to_id
在上面的查询中,如果 ticket_ticket.currently_assigned_to_id 为 null,则不返回 ticket_ticket 中的该行
> Records In tickets_ticket = 109
> Returned Records = 4 (out of 109 4 row has value for currently_assigned_to_id rest 105 are null )
> Expected Records = 109 (with nulll set for Assigned_To)
注意我正在尝试在同一个表上实现多个连接
解决方案
LEFT JOIN 不能杀死输出记录,你的问题在这里:
JOIN core_user on tickets_ticket.created_by_id = core_user.id
此连接会杀死不匹配的记录
LEFT JOIN core_user on tickets_ticket.created_by_id = core_user.id
推荐阅读
- angular - 如何使用 API 方法进行角度 matInput
- r - 如何在R中的数据集中创建一个窗口
- ios - 进行 REST 调用时,离子 ios 发出白页
- python - Kivy 和 Python:如何在同一画布上制作数学函数的不对称图形?
- lisp - 谁能向我解释为什么这个 lisp 代码不起作用?
- mongodb - AWS Lambda 连接到 MongoDB Atlas,提供失败的调用 API 操作状态代码 403
- git - 如何检查 git 存储库是否已克隆(最好使用 jgit)
- rust - 反向 Shell 中的 Rust 命令行历史
- javascript - 当我单击 Angular 组件中的按钮时无法更改 html 类
- python - 如何在标签中的 tkinter 中显示来自数据库的数据?