2015-08-08 251 views
0

所以,我有如下表:MySQL的轉義特殊字符

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),以便可以搜索以及以下語句(s ):

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 

總之,我只是希望該產品可以搜索其他語句(以及不僅僅是實際值/標題)。

幫助將被矇蔽!

+0

你可以使用'取代()'如果你始終 「」 炭 –

+0

@FabioCardoso人物,可以是任何東西。例如,客戶端搜索語句等於:usb - stick,它將被視爲產品1.基本上,我只是希望將特殊字符排列在值的內部,但唯一的原因是隻要信息是正確的信息加載。 – Bilal075

回答

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> %' 
+0

哦,上帝,我現在覺得很愚蠢。我實際上認爲這樣會更新專欄,但是因爲您選擇了這一行,所以沒有可能。非常感謝你。順便說一下,我怎麼能夠在多種條件下做到這一點? – Bilal075

+0

你想要什麼樣的多重條件? –

+0

其實,幾乎所有的特殊字符,<,> ./?:「{} []'!@#$%^&*()_- + = \ | – Bilal075