I'm creating a SQL query / subquery that will return a list of "class days". The query I'm using seems to not be recognizing the values from the subquery, and I cannot figure out why. If I run the subquery, I get the correct values. For some reason when I try to run the entire query & subquery together it doesn't work correctly, and I only get results for the 1st value in the comma separated list. If I run the query below, I only get results for tblclasses.cla_ID = 1234:
SELECT
*
FROM
classdays
WHERE
classdays.classday_classid IN (SELECT
CONCAT_WS(', ',
tclasses.cla_ID,
tclasses.cla_nextclass,
tclasses_1.cla_nextclass) AS nextclasses
FROM
tclasses
LEFT JOIN
tclasses AS tclasses_1 ON tclasses.cla_nextclass = tclasses_1.cla_ID
WHERE
tclasses.cla_ID = 1234)
However if I run just the subquery, it works and I get the list of values: 1234, 5678, 9012
(SELECT CONCAT_WS(', ',tclasses.cla_ID,tclasses.cla_nextclass,tclasses_1.cla_nextclass) AS nextclasses
FROM
tclasses
LEFT JOIN
tclasses AS tclasses_1 ON tclasses.cla_nextclass = tclasses_1.cla_ID
WHERE
tclasses.cla_ID = 1234)
And if I simply run the query with the values in the IN statement like so, it works as well:
SELECT
*
FROM
classdays
WHERE
classdays.classday_classid IN (1234, 5678, 9012)
Any help would be greatly appreciated, as for some reason I can't get the IN statement to run on those 3 comma separated values
Thanks!
INdoesn't work with comma-delimited strings. When you usex IN (subquery)it does an exact comparison to each row of the subquery result.CONCAT_WS, put them in separate rows usingUNION.'value1, value2, value3'.LEFT JOIN tclasses AS tclasses_1 ON tclasses.cla_nextclass = tclasses_1.cla_IDhow would that UNION query look?