2017-06-13 72 views
-1

這裏快速問題。我正在嘗試從3個不同的表中選擇peron_name和person_profession以及person member_role。 眼下,這是工作:SQL-2在一個聲明中加入

SELECT a.per_FirstName AS Nome 
    , pc.c1 as Profissao 
    , a.per_cls_ID as Membro 
FROM person_per AS a 
LEFT JOIN person_custom pc ON a.per_id = pc.per_ID 
ORDER BY pc.c1 
    , a.per_FirstName 

但它給我的member_role作爲member_code ID。我有另一個名爲list_lst的表,它將per_cls_ID與特定角色相關聯。我如何參考它?

現在,我的查詢打印:

JohnDoe  Doctor 2 
MaryJane Teacher 1 

我希望它打印:

JohnDoe Doctor Member 
MaryJane Teacher Visitor 

有人可以幫我嗎?

+1

你會與另一個做加盟。 http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/你也應該決定你使用哪個DBMS作爲mysql與sql server不是一回事。 –

+1

它如何能夠加入person_custom表,但無法加入list_lst表?這張桌子有什麼不同,你不能像第一張那樣加入它? –

回答

1

它是這樣的:

SELECT 
    a.per_FirstName AS Nome, 
    pc.c1 as Profissao, 
    l.COLUMNNAMEHERE as Membro 
FROM person_per AS a 
LEFT JOIN person_custom pc ON a.per_id = pc.per_ID 
LEFT JOIN list_lst l on l.per_cls_ID = a.per_cls_ID 
ORDER BY pc.c1, a.per_FirstName 
+0

謝謝!這工作 – yurividal