My question is the same as this one: Declare a variable in a 'read only' MySQL database, but I cannot use the solution there, as my query is too long and would require too many extra joins that slow down the query and make it time out.
I have read-only access to a MySQL database that I access using MySQL Workbench.
Should I expect to be able to use variables? So far I'm having no luck. I have tried the following:
set @project_ids := '674,705' -- RESTRICT PROJECTS; list must have no spaces
select distinct t.project_id, count(distinct(t.user_id)) as 'Total Users'
from table t
where find_in_set(@project_ids, t.project_id) -- RESTRICT PROJECTS
group by t.project_id
but I just get:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select distinct t.project_id at line 2'
I have also tried:
-- where find_in_set(pm.project_id, @project_ids) <> 0 -- RESTRICT PROJECTS: project_id field is in set of project ids
-- where pm.project_id in (674, 705) -- RESTRICT PROJECTS
I have also tried moving the set @project_ids := '674,705'
line to various places in the query, which also fail.
The reason I want to use a variable to restrict the list of project_ids is because the actual query is pretty long (275 lines, joins 20 tables together, includes 5 subqueries plus a union - it's a request from a colleague, and this is the only sticking point) and refers to this list 8 times - I don't want (them) to have to update this query in 8 places every time I/they run it.