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
在上面的查询中,如果tickets_ticket.currently_assigned_to_id为null,则不会返回tickets_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)
请注意,我正在尝试在同一张表上实现多个联接
答案 0 :(得分:1)
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
答案 1 :(得分:1)
首先,这不是您正在运行的实际代码。 from
子句前有一个逗号,会引起语法错误。如果您遗漏了where
子句,那么这将解释为什么您看不到任何行。
使用left join
时, first 表上的条件放在where
子句中。后续表的条件放在on
子句中。
也就是说,where
子句可能不是问题。我建议从第一个表开始使用left join
s以及表别名:
select oo.code as organization, cu.email as Created_By, a.email as Assigned_To,
from tickets_ticket tt left join
organizations_organization oo
on tt.organization_id = oo.id left join
core_user cu
on tt.created_by_id = cu.id left join
core_user a
on a.id = tt.currently_assigned_to_id ;
我怀疑您的数据模型中的数据是意外的-可能是不良的组织,也许是不良的created_by_id
。保留所有票证,看看有什么遗失。
也就是说,您可能应该在结果集中包含类似tt.id
之类的内容,以标识特定票证。