【问题标题】:Join two tables in postgresql if type of columns is different如果列的类型不同,则在 postgresql 中加入两个表
【发布时间】:2018-01-08 06:36:13
【问题描述】:

我需要连接两个表 table1 和 table2 其中 table1.id = table2.id 但 table1.id 是一个整数,而 table2.id 是一个字符串。

【问题讨论】:

  • 只是将 int 转换为字符串?.. 它当然不会执行,但是对于这样的任务,我认为你不能做任何正确的事情 - 对吧?

标签: psycopg2 postgresql-9.5


【解决方案1】:

试试 select * from a join b on a.id = nullif(b.id, '')::int

【讨论】:

    【解决方案2】:

    在比较之前,您需要将 table2.id 转换为整数。 (https://www.postgresql.org/message-id/371F1510.F86C876B@sferacarta.com)

    select * from table1,table2 where table1.id = cast(table2.id as int)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 2017-08-29
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多