0

我有 2 个表要连接,没有匹配的 id,如下所示;

表_a

id    name
1     moe
2     joe
3     bob
4     sue

表_b

id    accessid
10    moe99
11    joe53
12    bob51
13    312sue

我尝试使用 INSTR() 连接/加入这两个表。以下是我的代码;

select *
from table_a 
join table_b
on INSTR(table_a.name , table_b.accessid ) > 0

但是,我得到了这个

错误:函数 instr(字符变化,字符变化)不存在提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

我也尝试使用:

select * 
from table_a
join table_b
on table_a.name like '%' + table_b.accessid + '%'

select * 
from table_a, table_b
where table_a.name like '%' + table_b.accessid + '%'

但是这两个结果是;

查询没有返回匹配的行

任何人都可以帮助我吗?

4

2 回答 2

0

此错误消息:

错误:函数 instr(字符变化,字符变化)不存在提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

似乎是不支持的Postgresql错误。 所以使用这个函数(它也适用于MySql):instr()
position()

select *
from table_a join table_b
on position(table_a.name in table_b.accessid ) > 0

请参阅演示
结果:

| id  | name | id  | accessid |
| --- | ---- | --- | -------- |
| 1   | moe  | 10  | moe99    |
| 2   | joe  | 11  | joe53    |
| 3   | bob  | 12  | bob51    |
| 4   | sue  | 13  | 312sue   |
于 2019-12-04T15:47:36.780 回答
0

INSTR()文档说:

INSTR(str,substr)

返回字符串 str 中第一次出现子字符串 substr 的位置。这与 LOCATE() 的双参数形式相同,只是参数的顺序颠倒了。

您正在使用instr()错误的方法。你要:

select *
from table_a a
join table_b b on instr(b.accessid, a.name ) > 0

我发现这更容易理解LIKE(您也使用了错误的方式):

select *
from table_a a
join table_b b on b.accessid like concat('%', a.name, '%')
于 2019-12-04T15:34:56.290 回答