0

它有一列IsDisabled 我只想用于group_concatIsDisabled=0

目前我正在使用

select parentid,group_concat(REPLACE(WhatDoYouSell, ',', '')) sales from table_name
group by parentid

WhatDoYouSell如果只有IsDisabled=0该行,我想使用 group_concat列

我试过

select parentid,group_concat(REPLACE(WhatDoYouSell, ',', '')) sales from table_name
where IsDisabled=0 
group by parentid

似乎该查询忽略了所有group by parentid records如果其中一个recoedsIsDisabled =1

4

2 回答 2

3

使用内部条件尝试此查询 -

CREATE TABLE table_name(
  parentid INT(11) DEFAULT NULL,
  WhatDoYouSell INT(11) DEFAULT NULL,
  IsDisabled INT(11) DEFAULT NULL
);
INSERT INTO table_name(parentid, WhatDoYouSell, IsDisabled) VALUES
(1, 1, 0),
(1, 2, 1),
(1, 3, 0),
(2, 1, 1),
(2, 2, 1);

SELECT
  parentid,
  GROUP_CONCAT(IF(IsDisabled = 0, WhatDoYouSell, NULL)) sales
FROM
  table_name
GROUP BY
  parentid;

+----------+-------+
| parentid | sales |
+----------+-------+
|        1 | 1,3   |
|        2 | NULL  |
+----------+-------+
于 2012-11-23T07:22:22.570 回答
0

尝试这个:

SELECT parentid,GROUP_CONCAT(REPLACE(WhatDoYouSell, ',', '')) sales
FROM (SELECT parentid, WhatDoYouSell FROM  table_name 
WHERE IsDisabled=0 ) AS A
GROUP BY parentid
于 2012-11-23T07:15:03.423 回答