2013-10-19 101 views
0

我有兩個表這樣的:MySQL的合併2行成一個

Item 
------------------------------------- 
|ID | ImageURLID | BannerImageURLID | 
------------------------------------- 
| 1 |  1  |  2   | 
| 2 |  3  |  4   | 
| 3 |  5  |  6   | 
------------------------------------- 

ImageURL 
-------------------------------------- 
|ID | iOSURL | iOSRetinaURL | 
-------------------------------------- 
| 1 | www.test.at | www.testR.at | 
| 2 | www.bann.at | www.bannR.at | 
| 3 | www.test.at | www.testR.at | 
| 4 | www.bann.at | www.bannR.at | 
| 5 | www.test.at | www.testR.at | 
| 6 | www.bann.at | www.bannR.at | 
-------------------------------------- 

,我想輸出的外觀類似的東西:

------------------------------------------------------------------------------------- 
|ID | ImageURLiOS | ImageURLiOSRetina | BannerImageURLiOS | BannerImageURLiOSRetina | 
------------------------------------------------------------------------------------- 
| 1 | www.test.at | www.testR.at | www.bann.at  |  www.bannR.at  | 
| 2 | www.test.at | www.testR.at | www.bann.at  |  www.bannR.at  | 
| 3 | www.test.at | www.testR.at | www.bann.at  |  www.bannR.at  | 
------------------------------------------------------------------------------------- 

我嘗試了一些分組和內部連接但沒有真正作品。 是否有可能使用SQL來完成該操作,還是我必須執行一些PHP處理?

+1

多加入到同一個表:http://stackoverflow.com/questions/199953/how-do-you-join-on-the-same-table-twice-in-mysql – Tim

回答

2
select i.id, 
     u.iOSURL as ImageURLiOS, 
     u.iOSRetinaURL as ImageURLiOSRetina, 
     ur.iOSURL as BannerImageURLiOS, 
     ur.iOSRetinaURL as BannerImageURLiOSRetina 
from item i 
left join imageurl u on u.id = i.imageurlid 
left join imageurl ur on ur.id = i.bannerimageurlid 
+0

哦,是的感謝人! – perotom