使用INSTR()连接mySQL中的表
我有两个没有匹配id的表要连接,如下所示 表a使用INSTR()连接mySQL中的表,mysql,join,instr,Mysql,Join,Instr,我有两个没有匹配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
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(字符变化,字符变化)不存在
exist提示:没有与给定名称和参数类型匹配的函数。你
可能需要添加显式类型转换
我还尝试使用:
select *
from table_a
join table_b
on table_a.name like '%' + table_b.accessid + '%'
及
但这两个结果是:
查询没有返回匹配的行
有人能帮我吗 文件说明:
INSTR(str,substr)
返回字符串str中第一个出现的子字符串substr的位置。这与LOCATE()的两个参数形式相同,只是参数的顺序相反
您使用的instr()
方法错误。你想要:
select *
from table_a a
join table_b b on instr(b.accessid, a.name ) > 0
我发现使用这样的更容易理解(您也使用了错误的方法):
此错误消息:
错误:函数instr(字符变化,字符变化)不存在
exist提示:没有与给定名称和参数类型匹配的函数。你
可能需要添加显式类型转换
似乎是一个Postgresql错误,其中不支持instr()
。
因此,请使用该函数(该函数也适用于MySql):
请参阅。
结果:
请参阅CONCAT(),但我希望这是“一次性的”!
select *
from table_a a
join table_b b on instr(b.accessid, a.name ) > 0
select *
from table_a a
join table_b b on b.accessid like concat('%', a.name, '%')
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 |