Select 和 Concat_ws 记录组中的所有类型

Select with Concat_ws all type from group of record

使用 SQL,我可以做类似的事情:

SELECT T1.ID AS ORDER_ID, T2.CODE AS CODE  
FROM SCHEMA.TABLE1 AS T1 
LEFT JOIN SCHEMA.TABLE2 AS T2 ON T1.ID = T2.O_ID

输出:

ORDER_ID    CODE 
-----------------
25005CM73   AB 
25005CM73   CD 
25005CM73   EF 
25CMMM074   CD 
25CMMM074   AB 
25CMMM074   EF 
25CMMB075   GH 
25CMMB075   IG 
25CMMB075   KL

我想 return 每个 ORDER_ID 只有 1 行 CONCAT

预期输出:

ORDER_ID    CODE 
--------------------
25005CM73   AB,CD,EF
25CMMM074   CD,AB,EF
25CMMB075   GH,IJ,KL

一组ORDER_ID不知道怎么写代码。

尝试使用 GROUP_CONCAT:

SELECT 
  ORDER_ID,
  GROUP_CONCAT(CODE ORDER BY CODE SEPARATOR ',') as CODE
FROM
 (
  SELECT T1.ID AS ORDER_ID, T2.CODE AS CODE  
  FROM SCHEMA.TABLE1 AS T1 
  LEFT JOIN SCHEMA.TABLE2 AS T2 ON T1.ID = T2.O_ID
 ) as a
GROUP BY ORDER_ID;

注意: join时可以直接在查询中使用GROUP_CONCAT而不是子查询。