SQL 喜欢不拍_

SQL Like is not taking _

我只想查询包含这个确切字符串 string_ 的字符串。

我试过这个:

select * from table where name like "%string_%"

并且结果还包括 string-,但我只需要精确的 _

有什么帮助吗?

谢谢

_ 是 SQL 中单个字符的通配符。如果你想搜索 _ 你需要转义它:

select * 
from some_table 
where name like '%string\_%' escape '\' 

此外:字符串文字在 SQL 中被放入单引号中。如果您想使用更符合标准的 DBMS(Oracle、Postgres),您应该习惯于对字符串使用单引号。双引号仅用于标识符(首先应该避免)