简体   繁体   English

mysql-GROUP_CONCAT行转换为字符串

[英]mysql - GROUP_CONCAT rows into a string

I have a following result set: 我有以下结果集:

req_id |  p_id  | ai_result  |  hash | sku
1      |  4     | Match      |  XAN  | HOW
1      |  4     | Match      |  HXN  | HOW
1      |  4     | Non Match  |  123  | HOW

I need to have the following output 我需要以下输出

sku  | matched  |  non_matched
HOW  | XAN, HXN |  123

Here's as far as I could get: 这是我所能得到的:

SELECT sku, GROUP_CONCAT(hash) AS hash
FROM `sku_match` 
GROUP BY req_id, p_id

How can I distinguish rows based on ai_result column and put them separately. 如何基于ai_result列区ai_result并将它们分开放置。 Something like GROUP_CONCAT(hash) AS matched, GROUP_CONCAT(hash) AS non_matched ? GROUP_CONCAT(hash) AS matched, GROUP_CONCAT(hash) AS non_matched

Try using conditional aggregation: 尝试使用条件聚合:

SELECT
    sku,
    GROUP_CONCAT(CASE WHEN ai_result = 'Match' THEN hash END) AS matched,
    GROUP_CONCAT(CASE WHEN ai_result = 'Non Match' THEN hash END) AS non_matched
FROM sku_match
GROUP BY
    sku;

在此处输入图片说明

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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