gpt4 book ai didi

mysql - 使用 INSTR() 连接 mySQL 中的表

转载 作者:行者123 更新时间:2023-11-29 15:23:54 28 4
gpt4 key购买 nike

我有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

但是,我得到了这个

ERROR: function instr(character varying, character varying) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts.

我也尝试过使用:

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 + '%'

但这 2 个结果为;

Query returned no matching rows

有人可以帮我解决这个问题吗?

最佳答案

INSTR()文档说:

INSTR(str,substr)

Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.

您使用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, '%')

关于mysql - 使用 INSTR() 连接 mySQL 中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59179503/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com