【问题标题】:MySQL escaping special charactersMySQL转义特殊字符
【发布时间】:2015-08-08 13:40:12
【问题描述】:

所以,我有下表:

ID  product
1   usb-stick
2   blackberry
3   cup of tea
4   usb-stick 2.0
5   computer

并且每当我使用 LIKE 条件查看数据时都希望实现这一点,例如:

SELECT * FROM products WHERE product LIKE '% <value> %'

我会得到我正在寻找的结果。但是因为我的列中存储了特殊字符,所以我必须写下实际值才能将它们作为结果返回。

所以我的问题是,是否有任何方法可以转义特殊字符(存储在我的列中,例如:usb-stick),以便也可以搜索以下语句:

ID  product         title / keyword (seperated by comma) product must be searchable for
1   usb-stick       usb stick, usb        stick (notice the spaces)
2   blackberry      blackberry, black berry
3   cup of tea      cup of tee
4   usb-stick 2.0   usb stick 2.0, usb stick 2, usb-stick 2, etc
5   computer        computer

简而言之,我只是希望该产品也可以搜索其他语句(因此不仅仅是实际值/标题)。

希望有帮助!

【问题讨论】:

  • 你可以使用replace(),如果你一直使用"," char
  • @FabioCardoso 字符,可以是任何东西。例如,客户端搜索语句等于:usb - stick,这将被视为产品 1。基本上我只是希望在值内部排序特殊字符,但它存在的唯一原因是只要信息是正确的信息已加载。

标签: php mysql escaping character sql-like


【解决方案1】:

你可以使用replace()

SELECT * FROM products WHERE replace(product , '-', '') LIKE '% <value> %'

编辑,这根本不友好,但可以工作:

SELECT * FROM products WHERE
REPLACE(
    REPLACE(
        REPLACE(
            REPLACE(
                REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE(
                                REPLACE(
                                    REPLACE(
                                        REPLACE(
                                            REPLACE(
                                                REPLACE(
                                                    REPLACE(
                                                        REPLACE(
                                                            REPLACE(
                                                                REPLACE(
                                                                    REPLACE(
                                                                        REPLACE(
                                                                            REPLACE(
                                                                                REPLACE(
                                                                                    REPLACE(
                                                                                        REPLACE(
                                                                                            REPLACE(
                                                                                                REPLACE(
                                                                                                    REPLACE(
                                                                REPLACE(
                                                                    REPLACE(
                                                                        REPLACE(
                                                                            REPLACE(
                                                                                REPLACE(
                                                                                    REPLACE(
                                                                                        REPLACE(product, '\"', ''),
                                                                                    '.', ''),
                                                                                '?', ''),
                                                                            '`', ''),
                                                                        '<', ''),
                                                                    '=', ''),
                                                                '{', ''),
                                                                                                    '}', ''),
                                                                                                '[', ''),
                                                                                            ']', ''),
                                                                                        '|', ''),
                                                                                    '\'', ''),
                                                                                ':', ''),
                                                                            ';', ''),
                                                                        '~', ''),
                                                                    '!', ''),
                                                                '@', ''),
                                                            '#', ''),
                                                        '$', ''),
                                                    '%', ''),
                                                '^', ''),
                                            '&', ''),
                                        '*', ''),
                                    '_', ''),
                                '+', ''),
                            ',', ''),
                        '/', ''),
                    '(', ''),
                ')', ''),
            '-', ''),
        '>', ''),
    ' ', '-'),
'--', '-') like '% <value> %'

【讨论】:

  • 天哪,我现在感觉好傻。我其实以为这会更新列,但是因为你选择了行,所以不可能。非常感谢.. 顺便说一下,我怎么能在多种情况下做到这一点?
  • 你想要什么多重条件?
  • 其实几乎每个特殊字符,./?:;"{}[]'!@#$%^&*()_-+=\|
  • 我有解决方案但不友好,我编辑了我的答案。
  • 我看过,确实不友好。有没有机会用正则表达式或类似的东西来验证/清除它?
猜你喜欢
  • 1970-01-01
  • 2015-05-23
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 2012-10-07
相关资源
最近更新 更多