I have the following simplified and shortened tables:
table_a:
label type code
----- ---- ----
'----' 'ab' '0123' <-- label will be changed
'----' 'ab' '8234' <-- label will be changed
'----' 'ab' '9176' <-- label will not be changed
'----' 'cd' '4554' <-- label will be changed
'----' 'cd' '5122' <-- label will be changed
'----' 'cd' '7777' <-- label will not be changed
table_b:
label type code
----- ---- ----
'KOTA' 'ab' '01' <-- will match
'KOTA' 'ab' '8' <-- will match
'KOTA' 'ab' '923' <-- will not match
'RRHW' 'cd' '4' <-- will match
'WWUP' 'cd' '512' <-- will match
I update table a with this query:
UPDATE table_a AS a
RIGHT JOIN table_b AS b
ON a.type = b.type
AND LEFT(a.code, LENGTH(b.code)) = b.code
SET a.label = b.label;
It works but in the actual case in our database this is very slow. There’s no way I can change the database (adding an index or . . ). Is there a better (faster) way to update table a?