I’m having some trouble setting a user variable in MySQL and using it in the same query. I found one or two other questions similar to this, but couldn’t get any of their solutions to work in my case.
Here’s my query, stripped down a bit. I replaced values and names and stuff for simplicity, and removed some irrelevant parts, but left the basic structure for some context. Not really relevant but fyi I’m using CodeIgniter’s active record class to build the query.
SELECT * FROM (things, (SELECT @exp_time := IF(5 < 10, X, Y) as var))
JOIN more_things ON ...
WHERE ...
AND @exp_time < UNIX_TIMESTAMP()
AND `@exp_time` > 1319180316
ORDER BY ...
LIMIT 1 ...
The error I’m getting is: “Every derived table must have its own alias.”
Would really appreciate any assistance. Thanks!
Your actual error is because (as the error message explains) you haven’t specified an alias for your derived table, and this is required in MySQL (even if you never use the alias anywhere else in your query).
However you don’t need variables here. Now that your derived table actually has a name you have a way to refer to your value without needing to store it an a variable.