【问题标题】:Postgresql, sql command, join table with similar string, only string "OM:" is at the beginPostgresql, sql 命令,用相似字符串连接表,只有字符串“OM:”在开头
【发布时间】:2019-10-27 21:57:32
【问题描述】:

我想加入餐桌。

left join
c_store on o_order.customer_note = c_store.store_code

字段中的字符串几乎相同,只是在字段开头包含“OM:”,例如,来自o_order.customer_note的字段是

OM:4008

并且来自 c_store.store_code 是

4008

是否可以基于从 o_order.customer_note 中的每个字段中删除(或替换)来连接表 c_store.store_code?​​p>

我试过了

c_store on replace(o_order.customer_note, '', 'OM:') = c_store.store_code

但没有成功。我认为,这仅用于重命名列名,对吗?抱歉这个问题,我是新手。

谢谢。

【问题讨论】:

  • 替换方法写错了,应该是replace(o_order.customer_note,'OM:', '')

标签: sql postgresql replace


【解决方案1】:

在连接条件中使用字符串连接:

SELECT ...
FROM o_order o
LEFT JOIN c_store c
    ON o.customer_note = 'OM:' || c.store_code::text;

但这并不是说虽然上述逻辑可能会在短期内修复您的查询,但从长远来看,更好的修复方法是在您的数据库中设置正确的连接列。也就是说,希望能够仅在相等性上进行连接。这将使 Postgres 使用索引(如果存在)。

【讨论】:

    猜你喜欢
    • 2013-11-09
    • 2016-11-20
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2022-12-08
    • 2019-10-28
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多