gpt4 book ai didi

mysql - 如何在 MySql 中使用 COALESCE 来合并数据

转载 作者:行者123 更新时间:2023-11-29 07:55:31 25 4
gpt4 key购买 nike

这是我的 SQL 查询

DECLARE @strManualRefundIdList AS VARCHAR(MAX)

SELECT @strManualRefundIdList = COALESCE(@strManualRefundIdList + '|', '') + CONVERT(VARCHAR(MAX), ManualRefund_strReasonCode)
FROM tblManualRefunds WHERE ManualRefund_lngId IN ( 20 ,21 ,22 )

SELECT @strManualRefundIdList;

这就像枢轴一样,将所有行连接在一行中。我需要将其转换为 MySql 查询。

我尝试过这样

SELECT 
CONCAT(COALESCE(CONCAT(v_strManualRefundIdList, '|'), '') , ( ManualRefund_lngId))
INTO
v_strManualRefundIdList
FROM tblManualRefunds
WHERE Trans_lngId IN ( 20 ,21 ,22 ) ;

但它会抛出错误,错误代码:1172。结果由多行组成

如何翻译该查询。我是数据库新手。请帮我解决这个问题。

更新:

我发现的方法是分配给游标并循环并连接它。但这是唯一的方法吗?或者有更好的方法吗?

最佳答案

SELECT group_concat(ManualRefund_strReasonCode SEPARATOR '|')
FROM tblManualRefunds
WHERE manualRefund_lngId in (20,21,22)

SQL Fiddle

关于mysql - 如何在 MySql 中使用 COALESCE 来合并数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com