A Bogus
A Bogus

Reputation: 3930

Put list of integers into a variable then use it in select statement

I have a list of integers, that I want to use to generate a list of customers. I want to put the list into a variable, so I can use the list multiple times. How can this be done?

I have tried this -

set @CustIds = '001,002,003';
select * from customer 
where customer_id in (@CustIds); -- NOTE: customer_id is an integer

This is not working. How can I declare my list of customer ids to make this work?

Upvotes: 0

Views: 468

Answers (1)

Gurwinder Singh
Gurwinder Singh

Reputation: 39477

You're looking for find_in_set:

set @CustIds = '001,002,003';
select * from customer 
where find_in_set(customer_id, @CustIds) > 0;

Upvotes: 1

Related Questions