SQL连接问题

bba*_*bba 2 sql oracle

我有一个table_a包含数据列的表,如下所示:

.aaa01932
.asd02343
.lkj03342
Run Code Online (Sandbox Code Playgroud)

所以它始终以一个句点后跟3个字母开头,然后是2个数字(01,02等),后跟一些额外的数字.

我有另一个表格table_b,数据如下:

aaa01
asd02
lkj03
Run Code Online (Sandbox Code Playgroud)

因此它映射到数据中,table_a但是周期和末尾的额外数字被省略.

如何选择table_a加入table_b显示列中的数据"相等"的位置.平等我的意思是table_a.the_column像'%table_b.the_column%'.所以类似于:

Select * from table_a 
  join table_b on (table_a.the_column like '%table_b.the_column%');
Run Code Online (Sandbox Code Playgroud)

除了我不知道如何格式化like子句来接受里面的列.也许我需要以某种方式连接它?

Ton*_*ews 10

LIKE的语法是:

table_a.the_column like '%' || table_b.the_column || '%'
Run Code Online (Sandbox Code Playgroud)

另一种选择是SUBSTR:

table_b.the_column = substr(table_a.the_column,2,5)
Run Code Online (Sandbox Code Playgroud)

正如一些评论所说,SUBSTR是一种更好的方法,有两个原因:

  • 它可以编入索引:

    在table_a上创建索引(substr(the_column,2,5));

  • 它更准确:您只想在该位置匹配,而不是在列中的任何位置匹配

  • 该死,只有三秒钟!:) (2认同)