Mysql 满足某些条件时,在唯一键上合并字符串单元格

Mysql 满足某些条件时,在唯一键上合并字符串单元格,mysql,Mysql,我有一个这样的结果集,但我想以某种方式合并ID列重复的行,只保留值以“Y”或“N”结尾但前面的数字大于0的单元格。 我仅通过添加SUMagg函数和按行的唯一标识符分组(在我的情况下是TELEFON+CULOARE列)来处理数字 此结果的查询: ID TELEFON CULOARE piesa1 piesa2 piesa3 piesa4 1 telefon1 culoare1 0N 0N 0N 0N 1 telefon1

我有一个这样的结果集,但我想以某种方式合并ID列重复的行,只保留值以“Y”或“N”结尾但前面的数字大于0的单元格。 我仅通过添加
SUM
agg函数和按行的唯一标识符分组(在我的情况下是TELEFON+CULOARE列)来处理数字

此结果的查询:

ID  TELEFON     CULOARE    piesa1   piesa2  piesa3  piesa4
1   telefon1    culoare1    0N        0N      0N      0N
1   telefon1    culoare1    14Y       0N      0N      0N
2   telefon2    culoare2    0N        8Y      0N      0N
2   telefon2    culoare2    0N        0N      4Y      0N
3   telefon3    culoare3    0N        0N      0N      0Y
3   telefon3    culoare3    0N        0N      0N      0N
3   telefon3    culoare3    0N        0N      0N      0N
3   telefon3    culoare3    0N        0N      5Y      0N
4   telefon4    Neutru      8N        0N      0N      0N
4   telefon4    Neutru      0N        0N      1N      0N
4   telefon4    Neutru      0N        0N      0N      7Y
我的期望结果如下:

SELECT tt.id as `#`
,t.telefon as TELEFON
, IFNULL(c.culoare,'Neutru') as CULOARE
, (IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa1
,(IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa2
,(IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa3
,(IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa4 
,tt.total_piese
,tt.is_stoc_min
FROM telefon as t LEFT JOIN
total_piese as tt
ON t.id = tt.id_telefon
LEFT JOIN culoare as c
ON c.id  = tt.id_culoare
LEFT JOIN piesa as p
ON p.id = tt.id_piesa
ORDER BY t.id;
我有一个查询返回合并的行,但在值的末尾没有“Y”或“N”:

TELEFON     CULOARE   piesa1    piesa2  piesa3  piesa4
telefon1    culoare1    14Y       0N      0N      0N
telefon2    culoare2    0N        8Y      4Y      0N
telefon3    culoare3    0N        0N      5Y      0Y
telefon4    Neutru      8N        0N      1N      7Y

嗯,很难给你一个更好(或更优化)的答案,因为我们没有“基本数据”,但已经有了结果

无论如何,您可以将第一次尝试用作子查询,在piesa字段上使用
max
。(因为任何数字都将大于0,而Y大于N,因此
MAX
应提供所需的输出)

或者可能(未测试)在查询中直接使用max

select id as `#`, telefon, culoare,
max(piesa1) as piesa1,
max(piesa2) as piesa2,
max(piesa3) as piesa3,
max(piesa4) as piesa4
from
  (SELECT 
     tt.id 
    ,t.telefon as TELEFON
    ,IFNULL(c.culoare,'Neutru') as CULOARE
    ,(IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa1
    ,(IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa2
    ,(IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa3
    ,(IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa4 
    ,tt.total_piese
    ,tt.is_stoc_min
  FROM telefon as t LEFT JOIN
    total_piese as tt ON t.id = tt.id_telefon
  LEFT JOIN culoare as c ON c.id  = tt.id_culoare
  LEFT JOIN piesa as p ON p.id = tt.id_piesa) s
GROUP BY id, telefon, culoare
ORDER BY Id

如果我将您想要的结果更正为:第2行:
0n8y4y0n
/3行:
0n5y0y
/line4:
8n0n1n7y
是的,您是对的,根据您的评论调整了问题,感谢我现在拥有的数据,这两个查询都非常有效。将使用生产数据进行测试,如果一切正常,则标记为接受答案。谢谢,。
select id as `#`, telefon, culoare,
max(piesa1) as piesa1,
max(piesa2) as piesa2,
max(piesa3) as piesa3,
max(piesa4) as piesa4
from
  (SELECT 
     tt.id 
    ,t.telefon as TELEFON
    ,IFNULL(c.culoare,'Neutru') as CULOARE
    ,(IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa1
    ,(IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa2
    ,(IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa3
    ,(IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa4 
    ,tt.total_piese
    ,tt.is_stoc_min
  FROM telefon as t LEFT JOIN
    total_piese as tt ON t.id = tt.id_telefon
  LEFT JOIN culoare as c ON c.id  = tt.id_culoare
  LEFT JOIN piesa as p ON p.id = tt.id_piesa) s
GROUP BY id, telefon, culoare
ORDER BY Id
SELECT tt.id as `#`
,t.telefon as TELEFON
,IFNULL(c.culoare,'Neutru') as CULOARE
,max((IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa1
,max((IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa2
,max((IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa3
,max((IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa4 
FROM telefon as t LEFT JOIN
total_piese as tt
ON t.id = tt.id_telefon
LEFT JOIN culoare as c
ON c.id  = tt.id_culoare
LEFT JOIN piesa as p
ON p.id = tt.id_piesa
GROUP BY tt.Id, t.telefon, c.culoare
ORDER BY t.id;