I’m not sure this is possible directly in SQL. If it is possible, it is some nasty SQL.
I’ve gotten around this problem by using 2 SQL queries, one to get the necessary field ID’s, and the second to check those field_id_XX fields for the necessary values.
To do it purely in SQL you would need to be able to do a sub-query which not only built the column names, but also returned only the one column which you want for each row. You would need to pass the channel ID for that row to the sub-query, which I also don’t think you can do.
However, you might be able to achieve it with a MySQL stored function which you pass the channel ID to. I’m still not sure if you can return a value to be used as a column name though.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.