本文介绍了带下划线的MySQL LIKE查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表images:

+----+--------------+
| id |   img_path   |
+----+--------------+
| 1  | abc_1.jpg    |
| 2  | abc_2.jpg    |
| 3  | abcde_1.jpg  |
| 4  | abcde_2.jpg  |
| 5  | abcdef_1.jpg |
+----+--------------+

我想选择img_pathabc_开头的条目,因此我使用以下查询:

I would like to select the entries that img_path starts with abc_, so I use the following query:

SELECT id FROM images WHERE img_path LIKE 'abc_%'

但是它返回所有5行.我如何只返回id = 1& 2(哪个img_pathabc_开头)?

But it returns all 5 rows. How do I only returns id = 1 & 2 ( which img_path starts with abc_) ?

推荐答案

发现_是特殊字符.必须使用反斜杠来逃脱.

Found out that _ is a special character. Have to escape with backslashes.

SELECT id FROM images WHERE img_path LIKE 'abc\_%'

将按预期返回2行

这篇关于带下划线的MySQL LIKE查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:01