【问题标题】:Passing variable into mysql 8 select statement将变量传递给mysql 8 select语句
【发布时间】:2022-01-02 03:35:18
【问题描述】:

我正在尝试一个带有变量的简单选择语句。如果我更改类似 concat_ws('%', @S, '%'); ,该语句工作正常到一个字符串。似乎 select 语句没有拾取 SET 变量。帮助将不胜感激。我正在使用 Mysql80 工作台。

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

【问题讨论】:

    标签: mysql sql variables select


    【解决方案1】:

    只需使用CONCAT 来确保变量值的任一侧都有通配符。否则,使用第一个参数作为分隔符的 CONCAT_WS 在字符串结尾处返回双通配符 equivalent to single wildcard 并产生不希望的结果。

    LIKE 'product%%'
    
    LIKE 'product%'
    

    但是,CONCAT 会如您所愿返回通配符:

    LIKE '%product%'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-11
      • 2023-03-25
      • 2014-07-06
      • 1970-01-01
      • 2012-08-25
      • 2015-08-08
      • 2014-08-22
      • 1970-01-01
      相关资源
      最近更新 更多