2012-10-19 59 views
0

我有以下查詢:MySQL的返回值僅適用於ID 1

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 ...

謝謝,我不知道我失蹤了。

回答

1

您與內部查詢加盟:

SELECT account_id, account_fullname FROM accounts LIMIT 1 

,你必須LIMIT 1,它給你只有一列(其中可能有repaired_by = 1),然後要進行過濾,並且僅排在那裏repaired_by是2 ...而你沒有那個。

+0

謝謝,我刪除了限制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。

相關問題