Need to select only data that contains backslashes in MySQL

僤鯓⒐⒋嵵緔 提交于 2019-11-29 14:00:45

This command works for me when I'm running the command with php:

select * from exampletable where title like '%\\\\\\\\%' or title like '\\\\\\\\%' or title like '%\\\\';

The reason I include the beginning, anywhere, and end matches is to show that they are different. If you're looking for backslashes on the end, there only needs to be 4 backslashes. this is because there is nothing to escape after it. You use 8 backslashes on the other commands because they get parsed by php.

Your query works fine for me in MySQL 5.5.8:

mysql> create table exampletable (
  title varchar(255)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.15 sec)

mysql> 
mysql> insert into exampletable values ('Old King\\''s Road'),
  ('Running Down A Dream'),('Can\\''t Stop The Sun'),('This One''s For Me');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> 
mysql> select * from exampletable WHERE title LIKE '%\\\\%';
+---------------------+
| title               |
+---------------------+
| Old King\'s Road    |
| Can\'t Stop The Sun |
+---------------------+
2 rows in set (0.01 sec)

Try:

SELECT title FROM exampletable WHERE title LIKE '%\\%'

In the case of Ruby on Rails

search_value.gsub!(/\\/, "\\\\\\\\\\")
MyModel.where(["column_name ?","%#{search_value}%"])

If you don't want to use a wild card, add them.

search_value.gsub!(/%/, "\\%").gsub!(/_/, "\\_")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!