I want to use field name to determine the column that I will use to search for, but first I have to use the name to look up a column ID, then concatenate the column ID with a string to get the actual column name. I'm doing with with two queries now, but I'd like to combine them into a single query.
For example, I want to search for "fool=true". In the first table, I find out that values for "fool" are in column 1 of the second table. In the second table, I do the real search, for "field_id_1=true".
This table maps names to column IDs used in the second table.
exp_channel_fields
+field_id +field_name +
|---------|-----------|
|1 |fool |
|2 |money |
+---------+-----------|
This table has the data I want to search, but the column names don't quite match the ID from the prior step. I have to prepend "field_id_" to the ID I found before:
exp_channel_data
+entry_id+field_id_1 +field_id_2 +
|--------|-----------|-----------|
|837 |true |$500 |
|838 |false |$50,000 |
+--------+-----------+-----------+
Here's the first query I use, and I store the result into $myFieldID.
SELECT field_id as tField_id FROM exp_channel_fields
WHERE field_name = 'fool'
Then I use the result to build the second query:
SELECT
GROUP_CONCAT(entry_id ORDER BY entry_id SEPARATOR '|')
AS result_entries
FROM exp_channel_data
WHERE field_id_{$myFieldID} = "true"
Is there a way to combine these into a single query? Thanks in advance!