For example I have a table named TABLE
and it has a single field named FIELD
with values 1 and 2. Running this statement
SELECT GROUP_CONCAT(`FIELD`)
INTO @LIST
FROM `TABLE`;
and
SELECT @LIST;
returns
1,2
then I do an IF() statement
SELECT IF (1 IN(@LIST),"TRUE","FALSE");
returns
TRUE
BUT when I run this one
SELECT IF (2 IN(@LIST),"TRUE","FALSE");
it returns
FALSE
so I try this statement for a little debugging
SELECT IF (2 IN(1,2),"TRUE","FALSE");
and it returns my expected value
TRUE
So is there something that I missed? I need it to return TRUE.
@LIST
is effectively equivalent to a single string literal'1,2'
. But it's being evaluated in a numeric context, MySQL is evaluating that to be equal to integer1
. (TrySELECT @LIST+0
, andSELECT @LIST+4
, see what is returned. So, your predicate is effectivelySELECT 1 IN (1)
. In single quotes,SELECT '1' IN ('1,2')
results will be different. Bottom line: the comma inside the string (whether a literal or udv) is not going to be interpreted as part of SQL text. You can use string comparison functions on string literal or udv e.g.FIND_IN_SET
.