【问题标题】:LIKE with % on column namesLIKE 在列名上使用 %
【发布时间】:2012-10-27 19:14:31
【问题描述】:

这是导致语法错误的查询:

SELECT * 
FROM account_invoice,sale_order
WHERE sale_order.name LIKE %account_invoice.origin%

account_invoice.origin 字段包含 sale_order.name 的文本,以及其他文本,所以我需要在 account_invoice.origin 字符串的任何位置匹配 sale_order.name 字符串。

我使用的是 PostgreSQL 8.4。

【问题讨论】:

  • 看起来像一个 openerp 查询?不应该是“sale_order.name = account_invoice.origin”以避免将“SO123”与“SO1234”匹配

标签: postgresql sql-like


【解决方案1】:

试试这个

SELECT * 
FROM account_invoice,sale_order
WHERE sale_order.name LIKE '%'  || account_invoice.origin || '%'

% 需要单引号,因为模式是字符串。

||concatenation 的运算符。

【讨论】:

  • 谢谢,您的解决方案解决了我的问题。事实证明,我的 where 子句也倒过来了。应该是:WHERE account_invoice.origin LIKE '%' ||销售订单名称 || '%'
  • 为什么他们(postgres)不能只使用 + 运算符?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 2021-05-22
  • 2016-06-25
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
相关资源
最近更新 更多