Alexey Berezuev
Alexey Berezuev

Reputation: 793

Use blob variables in mysql

I have a mysql query like this:

set @category = '37,44,46';
SELECT * from products where category_id IN (@category);

But it returns Null. If I try to SELECT * from products where category_id IN (37,44,46) — it works.

@category is a BLOB variable (it's generating by another query).

I tried to write SELECT * from products where category_id IN (SELECT @category), but it doesn't work too :(

Upvotes: 0

Views: 795

Answers (1)

Raymond Nijland
Raymond Nijland

Reputation: 11602

You need something more like this

SET @category = '37,44,46';
SET @sql = CONCAT('SELECT * FROM products WHERE category_id IN (',@category,')');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt; 

check out the sqlfiddle http://sqlfiddle.com/#!2/4a1eb0/1

Upvotes: 3

Related Questions