我將下面的值插入到用戶表中,
~\`!@%23$%^%26*()-=_ {}[]|\\:;\"'<>?,./
然后用like查詢它,一無所獲,但是=作業正確,為什么該LIKE子句不起作用?
準備資料
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
insert into users(name) values('~`!@#$%^&*()-=_ {}[]|\\\\:;\\"\'<>?,./');
insert into users(name) values('test');
詢問
select * from users where name like '%~`!@#$%^&*()-=\_ {}[]|\\\\:;\\"\'<>?,./%'; // Got nothing
select * from users where name='~`!@%23$%^%26*()-=_ {}[]|\\\\:;\\"\'<>?,./'; // Got the inserted data
即使我逃脫了%and _,它仍然無法正常作業
select * from users where name like '%~`!@\#$\%^\&*()-=\_ {}[]|\\\\:;\\"\'<>?,./%'; // Still got nothing
期待什么
執行時
select * from users where name like '%~`!@#$%^&*()-=\_ {}[]|\\\\:;\\"\'<>?,./%'; // Got nothing
它應該只得到這個記錄
~\`!@%23$%^%26*()-=_ {}[]|\\:;\"'<>?,./
uj5u.com熱心網友回復:
您可以像這樣使用 concat (不確定那里的 MySQL 或其他 DB 型別,但原理是相同的):
select * from users
where name like
'%' || '~`!@#$%^&*()-=\_ {}[]|\\\\:;\\"\'<>?,./' || '%';
這是dbfiddle
更新。由于新行的新問題,我用ESCAPE.
select *
from users
where name like
'%~`!@#$#%^#&*()-=\_ {}[]|\\\\:;\\"\'<>?,./%' --<< escape # before % character
escape '#';
這是新的 dbfiddle
uj5u.com熱心網友回復:
您可以使用 INSTR:
SELECT * FROM users
WHERE INSTR(name, '~`!@#$%^&*()-=\_ {}[]|\\\\:;\\"\'<>?,./') > 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476281.html
標籤:sql
