所以,我有下表:
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
简而言之,我只是希望该产品也可以搜索其他语句(因此不仅仅是实际值(value)/标题)。
希望得到帮助!
最佳答案
您可以使用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> %'
关于php - MySQL转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893705/