[英]How to match the longest string and update the value?
I need to compare and match the longest match of two strings in two different tables and update one values if there is a closest match. 我需要比较和匹配两个不同表中两个字符串的最长匹配项,如果存在最接近的匹配项,则更新一个值。
Table 1 Table 2
stack1 stack2
ABCDEFG ABC
GHIJKLM ABCDE
PQRSUVW ABCDEF
I need to compare these two tables and match the closeet one and update Table 1 first row as ABCDEF the closest match, Please can anyone help me out. 我需要比较这两个表并匹配一个壁橱,并将表1第一行更新为最接近的匹配ABCDEF,请有人帮帮我。 I am stuck here. 我被困在这里。
Here is my query 这是我的查询
UPDATE table1 A
SET A.stack1 = (SELECT DISTINCT B.stack2
FROM table2 B
WHERE A.stack1 LIKE CONCAT(B.stack2,'%'))
WHERE name = 'name';
with this query am getting an error called 此查询正在收到一个错误,称为
ORA-01427: single-row subquery returns more than one row ORA-01427:单行子查询返回多个行
You need to make the subquery return only a single match (the longest one). 您需要使子查询仅返回一个匹配项(最长的匹配项)。 In your case MAX should do that. 以您的情况,MAX应该这样做。
UPDATE table1 A
SET A.stack1 = (SELECT Max( B.stack2 )
FROM table2 B
WHERE A.stack1 LIKE CONCAT(B.stack2,'%'))
WHERE name = 'name';
Also, you should think about the case where nothing matches. 另外,您应该考虑什么都不匹配的情况。
The ORA-01427 error is saying the subquery is returning more than one value -- even with the DISTINCT
. ORA-01427错误表示子查询返回多个值-即使使用DISTINCT
。
You need to correct the case(s) that are returning more than one distinct value. 您需要更正返回多个不同值的案例。 That could be with an aggregate function, like MAX
or MIN
, but without details I hesitate to make that recommendation. 可能有一个汇总函数,例如MAX
或MIN
,但是由于没有细节,我会毫不犹豫地提出建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.