0

I am trying to use defined variables in MySQL Workbench. I am using version 6.3 of Workbench. I am trying to run ad hoc queries that can have a value or multiple values:

If I have a single value, the query runs okay

set @Zdept_Id="548"

and then

Select *
from employees
where dept_Id in (@ZDept_Id)

but if I want to execute this for multiple departments, the query only returns values for the first number

set @Zdept_Id="548,221,1973,905"

and then

Select *
from employees
where dept_Id in (@ZDept_Id)

Dept_Id is an integer.

PS. These are reports that I run once a month, so automating them is not an option due to cost/benefit.

1
  • 1
    A variable value is ONE value. So you obtain Select * from employees where dept_Id in ('548,221,1973,905'), not Select * from employees where dept_Id in (548,221,1973,905) or Select * from employees where dept_Id in ('548','221','1973','905').
    – Akina
    Jan 14, 2020 at 10:38

1 Answer 1

1

I hope you are looking for FIND_IN_SET().

SELECT *
FROM employees 
WHERE FIND_IN_SET(dept_Id, @ZDept_Id);
1
  • @Vérace In Russian there exists a word which means both "to take away" and "to rob", and the difference is in context only...
    – Akina
    Jan 14, 2020 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.