如何在PostgreSQL中查找包含%'%的字符串?

huangapple go评论117阅读模式
英文:

How to find in postgres strings like %'%

问题

我需要在我的列字符串中搜索包含'的部分。

select * from app_user au where au.last_name like '%'%'

但类似这样的代码会导致:

SQL 错误 [42601]: 在位置 55 开始的未终止字符串文字

我还尝试了'%\\'%',但也不起作用,如何在postgres中转义'?

英文:

I need to search in my column strings which contains '

select * from app_user au where au.last_name like '%'%'

but something like that gives me

SQL Error [42601]: Unterminated string literal started at position 55

I tried also '%\'%', but it's not working too, how to escape ' in postgres?

答案1

得分: 0

CREATE TABLE app_user (last_name text)
INSERT INTO app_user VALUES ('ma''affee')
SELECT * FROM app_user au WHERE au.last_name LIKE '%''%'
last_name
ma''affee

fiddle

如您在示例中所见,INSERT语句也需要双单引号,但使用带参数的准备语句插入或选择时,无需再次双引号。

英文:

You need to double the single quote to get the result

CREATE TABLE app_user (last_name text)
INSERT INTO app_user VALUES ('ma''affee')
select * from app_user au where au.last_name like '%''%'
last_name
ma'affee

fiddle

As you see in the sample the INSERT needs also a double single quote, but prepare statements with parameters will take the need to double single quote away when inserting or selecting

答案2

得分: 0

要转义 ',您需要将其添加两次。使用 '' 来解决它。

select * from app_user au where au.last_name like '%''%'
英文:

To escape ', you need to add it twice. Use '' to resolve it.

select * from app_user au where au.last_name like '%''%'

huangapple
  • 本文由 发表于 2023年7月3日 18:43:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定