I'm trying to order a query based on the comma separated result of another query
SELECT t.field1, t.field2 /*, ...*/
FROM table t
/* ... */
ORDER BY
CASE WHEN NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t1.field1sub = t.field1)
THEN FIELD(t.field2, (SELECT field_order FROM table1 t1 WHERE t1.field1sub = t.field1))
ELSE FIELD(t.field2, (SELECT field_order FROM table1 t1 WHERE t1.field1sub = 0))
END
The important part of the query is this
FIELD(t.field2, (SELECT field_order FROM table1 t1 WHERE t1.field1sub = t.field1))
So basically, the subquery returns a comma separated string 1,2,3,7,9,4,10
, and I want to order by those fields first.
I know we can do for example ORDER BY FIELD(t.field,2,1,2,3)
, but how can we use a subquery that returns a string, to fill the order field?