search

Home  >  Q&A  >  body text

Will pass variables in SELECT statement in MySQL 8

I'm trying a simple select statement with variables. If I change the statement like concat_ws('%', @S, '%'); it works fine. to a string. It seems that the select statement does not select the SET variable. Thank you for your help. I am using Mysql80 workbench.

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

P粉764836448P粉764836448290 days ago534

reply all(1)I'll reply

  • P粉128563140

    P粉1285631402024-02-18 00:11:42

    Just use CONCAT to ensure wildcard characters on both sides of the variable value. Otherwise, first using CONCAT_WS as the delimiter argument returns a double wildcard at the end of the string, is equivalent to the single wildcard and produces undesired results.

    LIKE 'product%%'
    
    LIKE 'product%'
    

    However, CONCAT will return wildcards as you would expect:

    LIKE '%product%'
    

    reply
    0
  • Cancelreply