【问题标题】:Postgresql escape single quote in where clause [duplicate]Postgresql在where子句中转义单引号[重复]
【发布时间】:2016-12-06 15:48:54
【问题描述】:

所以我正在尝试运行这样的脚本:

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

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

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

有人知道我如何运行这样的查询吗?

【问题讨论】:

  • 试试双单引号 = "john ' 's"?
  • e'john\'s' 应该可以解决问题。 (e 前缀允许在字符串文字中使用 c 样式的反斜杠引用(和其他东西))
  • 单引号在 Postgres 中环绕字符串文字。要在字符串文字中转义单引号,请使用两个单引号:'John''s'
  • 字符串文字中的双单引号,即where owner.name = 'john''s'
  • 如果一切都失败了,请阅读手册:postgresql.org/docs/current/static/…

标签: sql postgresql


【解决方案1】:

双引号时可以转义单引号。 例如:

= 'john''s'

【讨论】:

  • 你能提一下如何转义双引号吗?
  • 有没有办法在单引号内也使用像 $1 这样的参数?
【解决方案2】:

试试这个

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2021-07-04
    • 1970-01-01
    • 2014-07-28
    • 2012-12-27
    • 2016-12-17
    • 2020-12-27
    相关资源
    最近更新 更多