mysql 如果列不为空则连接列

mysql concatenate columns if column is not null

这是 table 架构;

create table campaign (
    id INT NOT NULL,
    custom_param1 VARCHAR(15),
    custom_value1 VARCHAR(15),
    custom_param2 VARCHAR(15),
    custom_value2 VARCHAR(15),
    custom_param3 VARCHAR(15),
    custom_value3 VARCHAR(15),
    PRIMARY KEY (id)
)

这是我的模拟数据;

id  custom_param1   custom_value1   custom_param2   custom_value2   custom_param3   custom_value3
--  -------------   -------------   -------------   -------------   -------------   -------------
1   param1          value1          param2          value2          param3          value3
2   param1          value1          param2          value2          param3          value3
3   param1          value1          param2          value2          param3          value3
4   param1          value1          param2          value2          NULL            NULL
5   param1          value1          NULL            NULL            NULL            NULL

现在我正在尝试像格式一样连接参数和值列

param1=value1; param2=value2, param3=value3

如果一行只有两个参数和值,它应该是这样的;

param1=value1; param2=value2

好吧,我创建了一个可以连接行的查询,但如果其中一个为空,则整行变为空 - 据我所知,如果 concat 函数尝试连接 NULL 值字符串将完全变为无效的。这是我使用的查询。

select CONCAT(c.custom_param1, '=', c.custom_value1, '; ',
                c.custom_param2, '=', c.custom_value2, '; ',
                c.custom_param3, '=', c.custom_value3) as 'Custom Parameters'
from campaign as c;

如有任何帮助,我将不胜感激。还是谢谢

使用 ifnull http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull

select CONCAT(IFNULL(c.custom_param1, ''), '=', IFNULL(c.custom_value1, ''), '; ', IFNULL(c.custom_param2, ''), '=', IFNULL(c.custom_value2, ''), '; ', IFNULL(c.custom_param3, ''), '=', IFNULL(c.custom_value3,'')) as 'Custom Parameters' from campaign as c

您可以将 IFNULL(c.custom_param1, '') 更改为 IFNULL(c.custom_param1, 'NULL') 或您需要的任何内容。

更新信息param1=value1; =; = which should be param1=value1编辑:

select
CONCAT(
    IF(c.custom_param1 IS NOT NULL AND c.custom_value1 IS NOT NULL, CONCAT(c.custom_param1, '=', c.custom_value1, '; '), ''),
    IF(c.custom_param2 IS NOT NULL AND c.custom_value2 IS NOT NULL, CONCAT(c.custom_param2, '=', c.custom_value2, '; '), ''),
    IF(c.custom_param3 IS NOT NULL AND c.custom_value3 IS NOT NULL, CONCAT(c.custom_param3, '=', c.custom_value3), '')
) as 'Custom Parameters'
from campaign as c

示例:http://sqlfiddle.com/#!2/ef859f/2/0

使用ifnull检查列是否为空并将其替换为空白:

select CONCAT(ifnull(c.custom_param1,""), '=', ifnull(c.custom_value1,""), '; ',
                ifnull(c.custom_param2,""), '=', ifnull(c.custom_value2,""), '; ',
                ifnull(c.custom_param3,""), '=', ifnull(c.custom_value3,"")) as 'Custom Parameters'
from campaign as c;

更新:

select CONCAT(ifnull(c.custom_value1,concat(c.custom_param1,"=",c.custom_value1), ifnull(c.custom_value2,concat(";",c.custom_param2,"=",c.custom_value2)...) as 'Custom Parameters'
    from campaign as c;

尝试以下 SQL

select CONCAT(IFNULL(c.custom_param1,''), '=', (IFNULL(c.custom_value1,''), '; ',
                (IFNULL(c.custom_param2,''), '=', (IFNULL(c.custom_value2,''), '; ',
                (IFNULL(c.custom_param3,''), '=', (IFNULL(c.custom_value3,'')) as 'Custom Parameters'
from campaign as c;

如果值为null,看你想做什么,你可以试试

SELECT CONCAT(
c.custom_param1, '=', IFNULL(c.custom_value1, ''), '; ',
c.custom_param2, '=', IFNULL(c.custom_value2, ''), '; ',
c.custom_param3, '=', IFNULL(c.custom_value3, ''), '; ') as 'Custom Parameters'
FROM campaign as c;

会return

param1=value1; param2=value2; param3=;

或者您可以像这样排除整个值对....

SELECT CONCAT(
IFNULL(CONCAT(c.custom_param1, '=', c.custom_value1, '; '), ''),
IFNULL(CONCAT(c.custom_param2, '=', c.custom_value2, '; '), ''),
IFNULL(CONCAT(c.custom_param3, '=', c.custom_value3, '; '), '')) AS 'Custom Parameters'
FROM campaign as c;

这将 return

param1=value1; param2=value2;

希望对您有所帮助

SELECT ID,CONCAT_WS( COALESCE(CONCAT(custom_param1, custom_value1), ''),

         COALESCE(CONCAT(custom_param2,  custom_value2), ''),
       COALESCE(CONCAT(custom_param3, custom_value3), '')) campaign_survey
   FROM campaign;