I ran into a really strange problem today when using the MySQL function GROUP_CONCAT:
I have the following query:
SELECT SUM(total) FROM order WHERE customer_id='X' AND order_status_id IN ((SELECT GROUP_CONCAT(order_status_id SEPARATOR ',') FROM order_status WHERE profit='1'))
but that returns NULLL, however:
SELECT SUM(total) FROM order WHERE customer_id='X' AND order_status_id IN (1,2,3,4,5,6,7,8)
this works as well as the first query to concat the status id’s, grouped however they return NULL as total
GROUP_CONCAT()returns a string, which is a single value. TheIN()clause, although it accepts a comma-separated list of values, won’t take just any string you give it and then parse out the individual values.It treats the result of the
GROUP_CONCAT()as a single string unit, which could be a member of many other strings in a comma-separated list, but no rows matchorder_status_id = '1,3,5,6', which is what the RDBMS ultimately sees.If you want to use a query for that
IN()clause, use a subquery. TheIN ()clause knows how to deal with the rowset returned by the subquery and will treat it appropriately.