Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Mehrere MySQL-Tabellen-JOINS müssen die Ergebnisse einer Tabelle in einem einzigen Spaltenfeld zusammenfassen

Versuchen Sie so etwas:group_concat(comments order by id asc separator ' ') as comment

Beachten Sie, dass order by id sollte die ID des Kommentars sein (damit die Kommentare beim Verketten richtig geordnet sind).:

SELECT sfo.order_id, sfo.order_number, sfo.item_status, shipping.city, comments.comment
FROM nam_order_items AS sfo
JOIN sales_flat_order_address AS shipping ON shipping.parent_id = sfo.order_id
AND shipping.address_type =  'shipping'
JOIN
(
  select
    parent_id,
    group_concat(concat(created_at, ' - ', comment) order by entity_id asc separator ' ') as comment
  from
    sales_flat_order_status_history
  group by
    parent_id
) comments ON 
  comments.parent_id = sfo.order_id