这是表架构;
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
现在我正在尝试将params和values列连接起来,就像格式
一样param1=value1; param2=value2, param3=value3
如果一行只有两个参数和值,它应该是这样的;
param1=value1; param2=value2
我创建了一个查询,我可以连接行,但如果其中一个为null,则整行变为空 - 我知道如果concat
函数尝试连接NULL值,则字符串将完全变为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;
我会感激任何帮助。不管怎样,谢谢
答案 0 :(得分:7)
如果值为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;
将返回
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;
将返回
param1=value1; param2=value2;
希望有所帮助
答案 1 :(得分:1)
使用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
答案 2 :(得分:1)
使用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;
答案 3 :(得分:1)
尝试以下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;
答案 4 :(得分:0)
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;