MySQL MySQL查询中的Concat值(处理空值)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8530632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concat Values In MySQL Query(To Handle Null Values)
提问by ewom2468
I am writing a PHP and MySQL application in which i have to concatenate multiple column values into one single column.I would have used the concat() function,but it does not handle null values,and the concat_ws(),which does not return the result in the output i want. What i need can be achieved in the Oracle database like this:
我正在编写一个 PHP 和 MySQL 应用程序,其中我必须将多个列值连接到一个列中。我会使用 concat() 函数,但它不处理空值和 concat_ws(),它不返回结果在我想要的输出中。我需要的可以在 Oracle 数据库中实现,如下所示:
Select 'The Surname Is'||last_name from employees;
My Issue is how can i achieve this same result with MySQL..without using the above named functions?
我的问题是如何在不使用上述命名函数的情况下使用 MySQL 实现相同的结果?
回答by Filip Roséen - refp
回答by Minesh
You can also use CONCAT_WS function which takes care of NULL values
您还可以使用 CONCAT_WS 函数来处理 NULL 值
SELECT
CONCAT_WS(' ','The Surname Is',lastname)
FROM `employees`
回答by Martin Joiner
@Minesh: CONCAT_WS does not 'take care' of NULL values. To illustrate this...
@Minesh:CONCAT_WS 不会“处理”NULL 值。为了说明这...
CONCAT_WS("~",house.name,house.address,house.type)
In the above example, if house.addressis NULLthe returned result will not contain a neat double tilda (~~) as expected. It will be a tilda separated list with only 1 tilda. eg "fun House~mansion"
在上面的示例中,如果house.address为NULL,则返回的结果将不会像预期的那样包含整齐的双波浪号 (~~)。这将是一个 tilda 分隔的列表,只有 1 个 tilda。例如“欢乐屋~豪宅”
回答by ajreal
回答by Curlas
A little trick: Use empty string like separator with CONCAT_WS (Some times you wan't insert white spaces)
一个小技巧:在 CONCAT_WS 中使用像分隔符这样的空字符串(有时你不想插入空格)
CONCAT_WS('','The Surname Is:',lastname)
FROM `employees`