如何更新需要填充多个信息的单列表

时间:2016-02-18 14:59:53

标签: mysql

我有三张桌子如下

  1. manufacturing_detail_history
  2. manufacturing_products_history
  3. manufacturing_products
  4. manufacturing_detail_history有两列主键,描述

    manufacturing_products_history有两列primarykey和manu_product_id(主键是manufacturing_detail_history primarky键引用)

    manufacturing_products有三列manu_product_id,name,type

    需要在manufacturing_detail_history中添加新列manu_product_names,并使用逗号分隔值填充制造产品名称。

    不确定如何为每个manu_product_id获取关联的多个manu_product_name并使其以逗号分隔值并插入manu_product_names列

    提前致谢。

1 个答案:

答案 0 :(得分:0)

Select 
    T1.ID, T1.Desc, 
    T2.manu_product_id, 
    GROUP_CONCAT(T3.name ORDER BY T3.name ASC SEPARATOR ',')
From
    manufacturing_detail_history AS T1
inner join 
    manufacturing_products_history AS T2 ON T1.ID = T2.ID
inner join
    manufacturing_products AS T3 ON T3.manu_product_id = T2.manu_product_id
GROUP BY T1.ID, T1.Desc, T2.manu_product_id;