2016-10-08 100 views
-2

你好人我有一個問題,關於多個值的串聯進入單列,用逗號分隔,如下所示。如何連接sql中的多個值

例如有一個報表正在提取數據,但對於一列有5個值,因爲對於所有列重複5次相同的行,所以我想連接該特定列的所有5個值。

請讓我知道我該怎麼做。下面的 是提取5個值的查詢,我希望這些值只能用逗號分隔。

SELECT PAC.PACKAGED_ITEM_GID 
FROM SHIPMENT SH, ORDER_MOVEMENT OM, 
    ORDER_MOVEMENT_D OMD, S_SHIP_UNIT SSU, S_SHIP_UNIT_LINE SUL, 
    PACKAGED_ITEM PAC 
WHERE OM.SHIPMENT_GID = SH.SHIPMENT_GID 
AND OM.ORDER_MOVEMENT_GID = OMD.ORDER_MOVEMENT_GID 
AND OMD.S_SHIP_UNIT_GID = SSU.S_SHIP_UNIT_GID 
AND SSU.S_SHIP_UNIT_GID=SUL.S_SHIP_UNIT_GID 
AND SUL.PACKAGED_ITEM_GID = PAC.PACKAGED_ITEM_GID 
AND SH.SHIPMENT_GID = 'ULA/SAO.5000070143' 
+0

'SELECT PAC.PACKAGED_ITEM_GID從發貨SH,ORDER_MOVEMENT OM,ORDER_MOVEMENT_D OMD,S_SHIP_UNIT SSU,S_SHIP_UNIT_LINE SUL,PACKAGED_ITEM PAC WHERE OM.SHIPMENT_GID = SH.SHIPMENT_GID AND \t OM.ORDER_MOVEMENT_GID = OMD.ORDER_MOVEMENT_GID AND OMD.S_SHIP_UNIT_GID = SSU.S_SHIP_UNIT_GID AND SSU.S_SHIP_UNIT_GID = SUL.S_SHIP_UNIT_GID AND SUL.PACKAGED_ITEM_GID = PAC.PACKAGED_ITEM_GID AND SH.SHIPMENT_GID = 'ULA/SAO.5000070143' –

+0

提供樣品數據,期望的結果,並且查詢你在問題文本中使用*。 –

+0

感謝您查看它,但我明白了:) –

回答

0

MySQL的字符串聚合函數是GROUP_CONCAT

您使用的是1980年代使用的連接語法。爲什麼?你在哪裏學習?使用適當的ANSI連接代替:

SELECT GROUP_CONCAT(DISTINCT pac.packaged_item_gid) AS packaged_item_gids 
FROM shipment sh 
JOIN order_movement om ON om.shipment_gid = sh.shipment_gid 
JOIN order_movement_d omd ON omd.order_movement_gid = om.order_movement_gid 
JOIN s_ship_unit  ssu ON ssu.s_ship_unit_gid = omd.s_ship_unit_gid 
JOIN s_ship_unit_line sul ON sul.s_ship_unit_gid = ssu.s_ship_unit_gid 
JOIN packaged_item  pac ON pac.packaged_item_gid = sul.packaged_item_gid 
WHERE sh.shipment_gid = 'ula/sao.5000070143'; 

如你只選擇packaged_item_gid,這是可以在s_ship_unit_line,您可以單獨從該表中選擇和查找匹配的出貨量在IN條款。

SELECT GROUP_CONCAT(packaged_item_gid) as packaged_item_gids 
FROM s_ship_unit_line 
WHERE s_ship_unit_gid in 
(
    select ssu.s_ship_unit_gid 
    FROM shipment sh 
    JOIN order_movement om ON om.shipment_gid = sh.shipment_gid 
    JOIN order_movement_d omd ON omd.order_movement_gid = om.order_movement_gid 
    JOIN s_ship_unit  ssu ON ssu.s_ship_unit_gid = omd.s_ship_unit_gid 
    WHERE sh.shipment_gid = 'ula/sao.5000070143' 
);