加入字符串修改(替换、连接、trim)
Join With String Modifications (replace, concat, trim)
我无法让 join
工作。我有额外的空格和尾随内容,所以我认为 replace
、trim
、like
和 concat
(对于通配符)会起作用,但事实并非如此。
示例数据如下:
create table table1 (name varchar(200));
create table table2 (name varchar(200));
insert into table1 values
('A test: A Value'),
('A test: Another value');
insert into table2 values
('A Value: extra content'),
('Another Value: More content');
我希望 table1 的第 1 行与 table2 的第 1 行匹配,第 2 行也相同。这些应该匹配,因为 A test:
之后的内容 table 1 匹配 table 2.
的前导文本
我的尝试是:
select *
from table1 as t1
join table2 as t2
on trim(replace(replace(t1.name, 'A test:', ''), ' ', ' '))
like concat(trim(replace(t2.name, ' ', ' ')), '%')
这个 returns 虽然没有匹配,
它不起作用的原因是您试图在第一部分比第二部分短的情况下加入,例如:
在 'A Value' 喜欢 'A Value: extra content%'
因此,为了使其正常工作,您需要切换参数,以便条件变为:
在 'A Value: extra content' 喜欢 'A Value%'
我认为您应该更改连接子句,以便 % 与 t1.name 连接,而不是 t2.name。
这应该有效:
select *
from table1 as t1
join table2 as t2
on trim(replace(t2.name, ' ', ' '))
like concat(trim(replace(replace(t1.name, 'A test:', ''), ' ', ' ')), '%') ;
我无法让 join
工作。我有额外的空格和尾随内容,所以我认为 replace
、trim
、like
和 concat
(对于通配符)会起作用,但事实并非如此。
示例数据如下:
create table table1 (name varchar(200));
create table table2 (name varchar(200));
insert into table1 values
('A test: A Value'),
('A test: Another value');
insert into table2 values
('A Value: extra content'),
('Another Value: More content');
我希望 table1 的第 1 行与 table2 的第 1 行匹配,第 2 行也相同。这些应该匹配,因为 A test:
之后的内容 table 1 匹配 table 2.
我的尝试是:
select *
from table1 as t1
join table2 as t2
on trim(replace(replace(t1.name, 'A test:', ''), ' ', ' '))
like concat(trim(replace(t2.name, ' ', ' ')), '%')
这个 returns 虽然没有匹配,
它不起作用的原因是您试图在第一部分比第二部分短的情况下加入,例如:
在 'A Value' 喜欢 'A Value: extra content%'
因此,为了使其正常工作,您需要切换参数,以便条件变为:
在 'A Value: extra content' 喜欢 'A Value%'
我认为您应该更改连接子句,以便 % 与 t1.name 连接,而不是 t2.name。
这应该有效:
select *
from table1 as t1
join table2 as t2
on trim(replace(t2.name, ' ', ' '))
like concat(trim(replace(replace(t1.name, 'A test:', ''), ' ', ' ')), '%') ;