整理MySQL最后的“ AND”值

时间:2019-03-01 08:48:28

标签: mysql

我有一个场景:

这是我的剧本:

set @p1 = '01';
set @p2 = '02';
set @p3 = '03';
set @p4 = '04';
set @p5 = '05';
set @p6 = '';
set @p7 = '07';
set @p8 = '08';
set @p9 = '09';
set @p10 = '010';
set @p11 = '11';

SET @VAL =  concat_ws(' ',
IF(@p1='','',concat_ws(' ','F1=', @p1,' AND')),
IF(@p2='','',concat_ws(' ','F2=', @p2,' AND')),
IF(@p3='','',concat_ws(' ','F3=', @p3,' AND')),
IF(@p4='','',concat_ws(' ','F4=', @p4,' AND')),
IF(@p5='','',concat_ws(' ','F5=', @p5,' AND')),
IF(@p6='','',concat_ws(' ','F6=', @p6,' AND')),
IF(@p7='','',concat_ws(' ','F7=',@p7,' AND')),
IF(@p8='','',concat_ws(' ','F8=', @p8,' AND')),
IF(@p9='','',concat_ws(' ','F9=', @p9,' AND')),
IF(@p10='','',concat_ws(' ','F10=', @p10,' AND')),
IF(@p11='','',concat_ws(' ','F11=', @p11))
);

SET @res = CONCAT_WS(' ','Select ','@VAL');

PREPARE STMT FROM @res;
EXECUTE  STMT;

结果:

F1= 01  AND F2= 02  AND F3= 03  AND F4= 04  AND F5= 05  AND  F7= 07  AND F8= 08  AND F9= 09  AND F10= 010  AND F11= 11

此示例代码运行正常,但我的问题是此示例:

set @p1 = '01';
set @p2 = '02';
set @p3 = '03';
set @p4 = '04';
set @p5 = '05';
set @p6 = '';
set @p7 = '07';
set @p8 = '08';
set @p9 = '09';
set @p10 = '010';
set @p11 = '';

SET @VAL =  concat_ws(' ',
IF(@p1='','',concat_ws(' ','F1=', @p1,' AND')),
IF(@p2='','',concat_ws(' ','F2=', @p2,' AND')),
IF(@p3='','',concat_ws(' ','F3=', @p3,' AND')),
IF(@p4='','',concat_ws(' ','F4=', @p4,' AND')),
IF(@p5='','',concat_ws(' ','F5=', @p5,' AND')),
IF(@p6='','',concat_ws(' ','F6=', @p6,' AND')),
IF(@p7='','',concat_ws(' ','F7=',@p7,' AND')),
IF(@p8='','',concat_ws(' ','F8=', @p8,' AND')),
IF(@p9='','',concat_ws(' ','F9=', @p9,' AND')),
IF(@p10='','',concat_ws(' ','F10=', @p10,' AND')),
IF(@p11='','',concat_ws(' ','F11=', @p11))
);

SET @res = CONCAT_WS(' ','Select ','@VAL');

PREPARE STMT FROM @res;
EXECUTE  STMT;

结果:

F1= 01  AND F2= 02  AND F3= 03  AND F4= 04  AND F5= 05  AND  F7= 07  AND F8= 08  AND F9= 09  AND F10= 010  AND 

我需要从结果中删除最后一个单词是AND

任何更好的主意都会受到赞赏。

2 个答案:

答案 0 :(得分:2)

您可以使用TRIM()函数先删除尾随空格,然后再删除尾随AND

SET @VAL = TRIM('AND' FROM TRIM(@VAL));

SET @res = CONCAT_WS(' ','Select ','@VAL');

演示:Fiddle Demo #1

另一种方法是将数据存储到(临时)表中,并将GROUP_CONCAT()聚合函数与' AND '用作SEPARATOR。空值将在WHERE子句中过滤:

set @p1 = '01';
set @p2 = '02';
set @p3 = '03';
set @p4 = '04';
set @p5 = '05';
set @p6 = '';
set @p7 = '07';
set @p8 = '08';
set @p9 = '09';
set @p10 = '010';
set @p11 = '';

create temporary table k_v(
  k varchar(10),
  v varchar(10)
);

insert into k_v(k, v) values
  ('F1',   @p1),
  ('F2',   @p2),
  ('F3',   @p3),
  ('F4',   @p4),
  ('F5',   @p5),
  ('F6',   @p6),
  ('F7',   @p7),
  ('F8',   @p8),
  ('F9',   @p9),
  ('F10', @p10),
  ('F11', @p11);

SET @VAL = (
  SELECT GROUP_CONCAT(k, '= ', v SEPARATOR ' AND ')
  FROM k_v
  WHERE v <> ''
);

SET @res = CONCAT_WS(' ','Select ','@VAL');

PREPARE STMT FROM @res;
EXECUTE  STMT;

演示:Fiddle Demo #2

根据您的数据(@ p1,.. @ pN)来自何处,可能会有更优雅的解决方案。

答案 1 :(得分:1)

执行此操作的一种方法是将AND放入外部CONCAT_WS并在变量为空时生成IF语句NULL的结果,因为{ {1}会忽略{1}}的值(请参见manual)。这也将简化内部NULLCONCAT_WS的作用:

CONCAT_WS

输出:

CONCAT

Demo on dbfiddle