【问题标题】:MySQLi LIKE query with dynamic array [duplicate]带有动态数组的 MySQLi LIKE 查询[重复]
【发布时间】:2020-04-06 11:33:45
【问题描述】:

我有如下排列

这个1,这个2,这个3

有时

这个1,这个2,这个3,这个4

等等,

我想从我的表中查找结果 像 array1 或 array2 或 array3 这样的标签 或者有时像 其中像 array1 或 array2 或 array3 或 array4 这样的标签

我有 wall_tags 有如下行

this1,this2
this1
this1,this2,this3
etc

我当前的查询如下所示

SELECT * FROM tbl_wallpaper WHERE `wall_tags` like '%search_value%' ORDER BY id ASC LIMIT 3

我无法使其与 arrary 一起使用,如果有人可以帮助我,请告诉我。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    据我所知,您的查询有效 我添加了另一个执行相同操作的查询

    架构 (MySQL v8.0)

    CREATE TABLE tbl_wallpaper (
      `id` INTEGER,
      `wall_tags` VARCHAR(23)
    );
    
    INSERT INTO tbl_wallpaper
      (`id`, `wall_tags`)
    VALUES
      ('1', 'this1,this2,this3'),
      ('2', 'sometime'),
      ('3', 'this1,this2,this3,this4');
    

    查询 #1

    SELECT 
        *
    FROM
        tbl_wallpaper
    WHERE
        `wall_tags` LIKE '%this2%'
    ORDER BY id ASC
    LIMIT 3;
    
    | id  | wall_tags               |
    | --- | ----------------------- |
    | 1   | this1,this2,this3       |
    | 3   | this1,this2,this3,this4 |
    

    查询 #2

    SELECT 
        *
    FROM
        tbl_wallpaper
    WHERE
        FIND_IN_SET('this1',`wall_tags`) > 0
    ORDER BY id ASC
    LIMIT 3;
    
    | id  | wall_tags               |
    | --- | ----------------------- |
    | 1   | this1,this2,this3       |
    | 3   | this1,this2,this3,this4 |
    

    View on DB Fiddle

    【讨论】:

    • 您好,先生!谢谢你的好答案。我想你不明白我的问题。我想在 wall_tags LIKE this1 或 this2 或 this3 的地方得到结果。谢谢!
    • 我的搜索值在数组中。有时是 this1,this2 或有时是 this1,this2,this3 等。谢谢!
    • wall_tags 看起来像这样的逗号分隔字符串吗?您应该尝试构建您的 where 子句,在其中展开您的搜索字符串并从该 where 子句构建。
    • 谢谢!我在名为 wall_tags 的表中添加了示例行数据
    • 是的,我想是的,你必须这样做stackoverflow.com/a/11543329/5193536,mysql 不适合逗号分隔的列,这就是我们不使用它们的原因。见stackoverflow.com/questions/3653462/…
    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2013-03-14
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多