【问题标题】:MySQL Exclude Over Include in FIND_IN_SET()MySQL Exclude Over Include 在 FIND_IN_SET()
【发布时间】:2018-08-03 01:48:37
【问题描述】:

例如,我有一个这样的表:

----------------------------------------------------------------------------
|   id     |    title     |                        tags                    |
----------------------------------------------------------------------------
|    1     | Title 1      |  funny, adventure                              |
----------------------------------------------------------------------------
|    2     | Title Two    |  funny, short, horror                          |
----------------------------------------------------------------------------
|    3     | Title III    |  funny, short, adventure                       |
----------------------------------------------------------------------------

如何查询我想在结果中包含 funnyshort 但排除 horror?即使其中一个包含条件匹配,排除也会压倒它?

----------------------------------------------------------------------------
|   id     |    title     |                        tags                    |
----------------------------------------------------------------------------
|    1     | Title 1      |  funny, adventure                              |
----------------------------------------------------------------------------
|    3     | Title III    |  funny, short, adventure                       |
----------------------------------------------------------------------------

【问题讨论】:

    标签: mysql left-join find-in-set


    【解决方案1】:

    试试这个 -

    SELECT * FROM table_name WHERE NOT find_in_set('horror',tags) <> 0 
    

    【讨论】:

    • 它仍然会返回带有恐怖标签的记录。
    • 从逗号分隔值中删除空格,然后使用 find_in_set 运行查询
    • 你也可以试试这个 SELECT * FROM table_name WHERE tags NOT LIKE "%horror%"
    • 感谢 Saurabh。是的,现在有效。也在使用 REGEXP。由于 LIKE 子句的性能问题,我决定使用 REGEXP。 SELECT * FROM table_name WHERE tags NOT REGEXP 'horror';
    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多