简体   繁体   English

MySQL GROUP_CONCAT(DISTINCT ...) 忽略数据

[英]MySQL GROUP_CONCAT(DISTINCT …) ignores data

i am concatenating multiple rows and columns of a table storing postal addresses using GROUP_CONCAT .我正在使用GROUP_CONCAT连接存储邮政地址的表的多行和多列。

My table looks like this (simplified):我的表看起来像这样(简化):

id | street  | hNumber
-----------------------
1  | streetA | 1       
-----------------------
2  | streetB | 2      
-----------------------
3  | streetC | NULL    
-----------------------

In some cases i need to concatenate addresse which i am doing like this: GROUP_CONCAT(DISTINCT table.street, ' ', table.hNumber SEPARATOR ', ') as street在某些情况下,我需要像这样连接地址: GROUP_CONCAT(DISTINCT table.street, ' ', table.hNumber SEPARATOR ', ') as street

This for example would give me StreetA 1, StreetB 2 as a result which is just what i want.例如,这会给我StreetA 1, StreetB 2结果,这正是我想要的。 Sadly this method does not work for cases where for example the hNumber is empty, in those cases it just returns street as NULL .遗憾的是,这种方法不适用于例如hNumber为空的情况,在这些情况下,它只会将street作为NULL返回。 For rows in which the hNumber table cell is empty i would like to see it just skip over the empty field and return street as StreetA 1, StreetC .对于hNumber表格单元格为空的行,我希望看到它只是跳过空字段并将street作为StreetA 1, StreetC 返回

I hope someone can point me in a direction to approach this problem the proper way.我希望有人能指出我以正确方式解决这个问题的方向。

MySQL skips any of those rows that contain a NULL value. MySQL 会跳过任何包含 NULL 值的行。 The short answer is to anticipate a possible NULL value for a column and prepare for it.简短的回答是预测列的可能 NULL 值并为此做好准备。 eg:例如:

SELECT GROUP_CONCAT(DISTINCT `street`, ' ', IFNULL(`hNumber`,"") SEPARATOR ', ')

(create empty string, if hNumber is NULL) (创建空字符串,如果 hNumber 为 NULL)

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

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