2011-09-30 41 views
0

我要加入這個MySQL表:需要一個查詢把某些MySQL數據庫列插入行

TABLE1

id pagetitle 
1 remodeling 
2 handywork 
3 aesthetics 

有了這一個:

TABLE2

id contentid tmplvarid value 
1 1   1   Jaime 
2 1   2   img/remodeling.jpg 
3 2   1   Alex 
4 2   2   img/handywork.jpg 
5 3   1   Karla 
6 3   2   img/aesthetics.jpg 

輸出:

id pagetitle author image 
1 remodeling Jaime img/remodeling.jpg 
2 handywork Alex img/handywork.jpg 
3 aesthetics Karla img/aesthetics.jpg 

注:表1和表2之間的關係是:Table1.id = Table2.contentid

如果有幫助... tmplvarid 1是作者和tmplvarid 2圖像

什麼是sql查詢我可以用來完成這個?

回答

1
select t1.id, 
     t1.pagetitle, 
     (select value from TABLE2 where contentid = t1.id and tmplvarid = 1) as author, 
     (select value from TABLE2 where contentid = t1.id and tmplvarid = 2) as image 
    from TABLE1 t1 
+0

非常感謝你!這工作! – user972356