Q:

Trimming Mysql last "AND" value

I have a scenario:
Here's my script:
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;

Result:
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

This sample code is working fine but my problem is this example:
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;

Result:
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 

I need to remove if the last word is AND from the result. 
Any better idea is much appreciated. 

A:

One way to do this is to put the AND into the outer CONCAT_WS and make the result of the IF statements NULL when the variable is empty, since NULL values will be ignored by CONCAT_WS (see the manual).  This will also allow simplifying the inner CONCAT_WS to CONCAT:
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(' AND ',
IF(@p1='',NULL,concat('F1=', @p1)),
IF(@p2='',NULL,concat('F2=', @p2)),
IF(@p3='',NULL,concat('F3=', @p3)),
IF(@p4='',NULL,concat('F4=', @p4)),
IF(@p5='',NULL,concat('F5=', @p5)),
IF(@p6='',NULL,concat('F6=', @p6)),
IF(@p7='',NULL,concat('F7=',@p7)),
IF(@p8='',NULL,concat('F8=', @p8)),
IF(@p9='',NULL,concat('F9=', @p9)),
IF(@p10='',NULL,concat('F10=', @p10)),
IF(@p11='',NULL,concat('F11=', @p11))
);

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

PREPARE STMT FROM @res;
EXECUTE  STMT;

Output:
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

Demo on dbfiddle

