Table User contains the fields id, name, fruit, veggies, meat.
Table FruitType contains the fields id, type, healthy, user.
Let’s presume the table User looks like this:
1, Marvin, Apple, Carrot, Pork
2, Zorlax, Blueberry, Lettuce, Lamb
.. while the FruitType table looks like this:
1, fruit, yes, 1
2, veggies, yes, 2
What I would like is to be able to JOIN them, and search for a value in Users, in a specific field, depending on the VALUE from the “type” field in FruitType. For example, I would like to be able to do this:
SELECT User.'type' FROM User
LEFT JOIN FruitType ft ON (ft.user = User.id) etc.. conditions and so on...
Basically, I want the ‘type’ part to take the value of the ‘type’ field in the FruitType table. Something like a field variable, in essence.
I hope I’m being clear enough. Here is a real example I’m working on right now, in case this seems too confusing, although the real one might do even more damage:
IF (trigger_by_date IS NOT NULL,
date_add(books.trigger_by_date, INTERVAL trigger_by_date_add DAY),
date_add(trigger_step_created_at, INTERVAL trigger_by_step_add DAY))
as deadline
In this case, trigger_by_date is the variable. If it’s not null, simply use its value as the field identifier.
Edit:
For clarity’s sake: there is no “type” column in User. The “type” in the example query should be replaced by the actual value of type in the FruitType table. So if a condition yielded the result FruitType.type = fruit, I want the query to SELECT User.fruit. If a condition gave FruitType.type = veggies, I want the query to SELECT User.veggies. I hope it is clearer now. I basically want the field I am selecting to depend on the type in FruitType.
The solution was rewriting the query with a couple of advanced subqueries.