Oracle SQL比较两个表数据

时间:2018-05-09 20:48:51

标签: sql oracle

我的oracle db中有两个表A,B,我希望根据唯一字段(userid)比较两个表的数据,但B表包含用户ID为Puserid(所有ID附加P)

如何在两种表中使用上述情况的情况来检索数据?

2 个答案:

答案 0 :(得分:2)

您可以使用ltrim

select a.*, b.*
  from tableA a join tableB b on ( a.userid = ltrim(b.userid,'P') );

答案 1 :(得分:2)

技术上," P"是 prepended ,而不是附加。

您可以使用||

from a join
     b
     on b.userId = 'P' || a.userId

这可能会对性能产生重大影响。如果能够,则应在b(可能是虚拟列)中定义新列。以上内容可以使用b(userid)上的索引。

您也可以将其命名为:

from a join
     b
     on a.userId = substr(b.userId, 2)

这可以使用a(userid)上的索引。

相关问题