繁体   English   中英

MySQL将2行合并为一行

[英]MySQL Combine 2 rows into one

我有两张桌子:

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处理?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM