开发者

Need a query to put certain mySQL database columns into a row

I need to join this mySQL table:

TABLE1

id  pagetitle
1   remodeling
2   handywork
3   aesthetics

With this one:

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

To output this:

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

Note: The relation between Table1 and Table2 is开发者_如何转开发: Table1.id = Table2.contentid

If it helps ...tmplvarid 1 is author and tmplvarid 2 is image

What is the SQL query I can use to accomplish this?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜