2014-08-30 51 views
0
table1 
id firstname 
------------- 
1 Elon 
2 Steve 


table2 
id profession 
------------- 
1 Entrepreneur 
2 Engineer 


table3 
firstname profession 
------------- 
1 2 
2 1 

需要的結果是:如何從一個MySQL查詢中的不同表中進行選擇?

firstname profession 
------------- 
Elon Engineer 
Steve Entrepreneur 

如何從一個MySQL查詢不同的表選擇? 如何從一個MySQL查詢中的不同表中進行選擇?

+1

: //www.google.co.in/?gfe_rd=cr&ei=cHoBVLKOAYfW8gfqqYDoCw&gws_rd=ssl#q=How+can+I+select+from+different+tables+in+one+MySQL+query%3F – gvgvgvijayan 2014-08-30 07:17:29

+0

在相關中使用JOIN列? – hd1 2014-08-30 08:35:52

+0

使用jois:左,右或內部 – 2014-08-30 08:40:44

回答

1

此聲明應該給你你需要的結果。

select t1.firstname, t2.profession from table1 t1 join table3 t3 on t1.id=t3.firstname join table2 t2 on t3.profession = t2.profession 
+0

#1146 - 表'test.t2'不存在 – user2429282 2014-08-30 07:35:09

+0

@ user2429282在查詢中產生錯誤,您可以再試一次嗎? – Jens 2014-08-30 07:37:05

+0

返回空::( – user2429282 2014-08-30 07:41:24

0

你可以這樣做:

SELECT t1.firstname, t2.profession 
    FROM table1 as t1 
    LEFT JOIN table3 as t3 ON t1.id = t3.firstname 
    LEFT JOIN table2 as t2 on t2.id = t3.profession 

這一個工程,要在這裏請學習一些SQL查詢,如果您發佈在谷歌搜索的問題意味着你得到很多幫助HTTPS之前測試

+0

#1054 - '字段列表'中的未知列't2.profession' – user2429282 2014-08-30 08:19:34

+0

比你沒有列用這個名字,爲什麼你不給我們: describe table1; describe table2; describe table3; – 2014-08-30 08:22:57

+0

我編輯我的帖子,這個查詢已經過測試,它的工作原理 – 2014-08-30 08:30:14

相關問題