Magnillo
Magnillo

Reputation: 1

Passing variable into mysql 8 select statement

I am trying a simple select statement with a variable. The statement works fine if I change the like concat_ws('%', @S, '%'); to a string. It seems that the select statement is not picking up the SET variable. Help would be much appreciated. I am using Mysql80 workbench.

SET @S = "product";
SELECT distinct idproducts FROM mgjtest.vorutaflamedsamheit
WHERE productname like concat_ws('%', @S, '%');
````````````````````````````````````````````````````````````````

Upvotes: 0

Views: 58

Answers (1)

Parfait
Parfait

Reputation: 107747

Simply use CONCAT to ensure wildcards on either side of variable value. Otherwise, CONCAT_WS which uses first argument as separator returns double wildcards at the end of string which is equivalent to single wildcard and yields undesired results.

LIKE 'product%%'

LIKE 'product%'

However, CONCAT will return wildcards as you expect:

LIKE '%product%'

Upvotes: 1

Related Questions