我收到了以下问题:
SELECT ur.repair_id, ur.repair_complete, ur.repair_noted_by_client, ur.repair_problem, ur.added_at, ur.added_by, ur.repaired_at, ur.repaired_by,
aa.account_fullname AS added_by_name,
ar.account_fullname AS repaired_by_name
FROM units_repairs AS ur
LEFT JOIN (SELECT account_id, account_fullname FROM accounts LIMIT 1) AS aa ON aa.account_id = ur.added_by
LEFT JOIN (SELECT account_id, account_fullname FROM accounts LIMIT 1) AS ar ON ar.account_id = ur.repaired_by
WHERE ur.unit_id = 1
仅在account_id = 1
时返回全名。如果说repaired_by = 2
,则说NULL
...
谢谢,我不知道我错过了什么。
答案 0 :(得分:1)
您正在加入内部查询:
SELECT account_id, account_fullname FROM accounts LIMIT 1
你有LIMIT 1
,它只给你一行(可能有repaired_by = 1),然后你想要过滤并只得到repaired_by为2的行...而你没有那一个。
答案 1 :(得分:0)
可能没有记录满足行中的条件repaired_by = 2
,
LEFT JOIN (SELECT account_id, account_fullname FROM accounts LIMIT 1) AS ar ON ar.account_id = ur.repaired_by
因此,ar.account_id = ur.repaired_by
返回NULL。