英文:
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 |
如您在示例中所见,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 |
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 '%''%'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论