SQL Postgresql 在 where 子句中转义单引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38700966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:45:36  来源:igfitidea点击:

Postgresql escape single quote in where clause

sqlpostgresql

提问by Eugen

so I am trying to run a script like this one:

所以我试图运行这样的脚本:

select id
from owner 
where owner.name = "john's"

and I am getting this error: ERROR: column "john's" does not exist.

我收到此错误:ERROR: column "john's" does not exist

Also I tried like this: where owner.name = 'john\'s', but it dit not work

我也试过这样:where owner.name = 'john\'s',但它不起作用

Anyone knows how I can run a query like this one?

任何人都知道我如何运行这样的查询?

回答by manuzi1

You can escape single quotes when you double them. For example:

将单引号加倍时可以转义单引号。例如:

= 'john''s'

回答by Paarth

Try this

尝试这个

select id
from owner 
where owner.name = (E'john\'s')::text

Update: we can escape most of the characters using this statement

更新:我们可以使用此语句转义大部分字符

select id
from owner 
where owner.name = (E'john\character you want to escape's')::text