0

Here is the simplified query that doesn't work.

SET @abc = CONCAT('%','string','%');

SET @query = CONCAT('SELECT * 
FROM table 
WHERE column LIKE ',@abc);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

I need to use CONCAT with SELECT because there lots of other variables in real query.

Real query works fine when I use some simple COLUMN=xyz in WHERE clause. But nothing works when I try to use LIKE %xyz%...

user3221449
  • 71
  • 2
  • 10

1 Answers1

1

Use it like this

SET @abc = CONCAT('"%','string','%"');

SET @query = CONCAT('SELECT * 
 FROM table 
WHERE column LIKE ',@abc);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Check the first line I have added " to show @abc like "%string%"

Arun Krish
  • 2,153
  • 1
  • 10
  • 15