简体   繁体   中英

Union all values in Column

I'm pretty new to SQL, I hope someone can give me a lead on this issue. Or if this even possible. In the table below:

order_id      order_contact      order_location       order_value
1             Tom's Business      123 Street           100
1             John's Management   123 Street           100
2             Tim's Business      543 Avenue           50
3             Phil's Business     789 Avenue           50

My question is: Is it possible to join/union those 2 records when the order_id is 1? The contact is the only difference between the record. I worked with Union but I think that is only possible with different columns and not rows. I would like it to show something like this:

order_id      order_contact        order_location      order_value
1             Tom's Business       123 Street          100
              John's Management                

2             Tim's Business      543 Avenue           50
3             Phil's Business     789 Avenue           50

Is this even possible? Thanks for your help

You can use the following solution using GROUP_CONCAT :

SELECT order_id,
    GROUP_CONCAT(DISTINCT order_contact SEPARATOR ' ') AS order_contact,
    GROUP_CONCAT(DISTINCT order_location SEPARATOR ' ') AS order_location,
    GROUP_CONCAT(DISTINCT order_value SEPARATOR ' ') AS order_value
FROM table_name 
GROUP BY order_id

demo: http://sqlfiddle.com/#!9/78d7ea/2/0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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