0

Possible Duplicate:
Parameterizing an SQL IN clause?

Is it possible to use a variable in a IN clause to get the wanted results ?

hard-coded example;

SELECT 1 FROM `test` WHERE test_name IN ('Test1', 'Test2');

Desired solution:

SET @test_names = 'Test1, Test2';
SELECT 1 FROM `test` WHERE test_name IN (@test_names);

Or is there some other way to achieve this ?

SOLUTION:

SET @test_names = 'Test1,Test2';
SELECT 1 FROM `test` FIND_IN_SET(test_name, @test_names);

Notice that the @test_names does not have any spaces in it !

Community
  • 1
  • 1
Blottt
  • 119
  • 1
  • 13

1 Answers1

0

just another solution. (Got it by trial and error ^^)

SQLFIDDLE DEMO

SET @looking_for = "('Test1', 'Test2')";

SET @sql = CONCAT_WS(' WHERE test_name IN ', 'SELECT * FROM test', @looking_for);
PREPARE preptest FROM @sql;
EXECUTE preptest;
Varon
  • 3,736
  • 21
  • 21
  • Indeed this is another solution, i'll rather use the other one, that one don't have to create hard-coded SQL – Blottt Nov 20 '12 at 20:24