2

我在 MySQL 表上有这些数据cart

在此处输入图像描述

我想运行一个输出如下的查询:

Advance Technique Conditioner (2), Advance Technique Gel (1)

所以我正在做一个group_concat查询,并试图查询输出我想要的输出。这是输出最接近所需的查询:

select concat(group_concat(product separator ', '), group_concat(quantity separator ', ')) as 'prod' from cart where debt_no='0000001'

但正如预期的那样,它的输出如下:Advance Technique Conditioner, Advance Technique Gel2, 1

如何获得所需的输出?

4

3 回答 3

7

CONCAT()应该在里面GROUP_CONCAT()

GROUP_CONCAT(CONCAT(product, ' (', CAST(quantity AS CHAR(15)), ')') SEPARATOR ', ')

请记住,默认长度GROUP_CONCAT()是 1024。因此,如果要更改限制,请执行以下行,

SET [GLOBAL | SESSION] group_concat_max_len = val; -- val is the new length
于 2013-06-05T02:23:07.390 回答
2
SELECT GROUP_CONCAT(CONCAT(product, ' (', quantity, ')') SEPARATOR ', ')
于 2013-06-05T02:22:54.530 回答
1

你需要先做concat(),然后是group_concat()

select group_concat(concat(product, ' (', quantity, ')') separator ', ') as prod
from cart where debt_no='0000001'
于 2013-06-05T02:24:49.910 回答