Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to perform UPDATE with recursive LIKE operating on multiple rows?

Given this tableA:

pattern data
abcxyz 1
abczys 2
jklabc 42
jkladc 42

And another tableB:

pattern data
abc 100
jkl 200

how do I construct a query that will transform the first table into:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pattern data
abcxyz 100
abczys 100
jklabc 200
jkladc 200

Basically, UPDATE tableA set tableA.data = tableB.data from tableB where tableA.pattern LIKE (SELECT tableB.pattern from tableB group by tableB.pattern) || '%' but with LIKE accepting multiple rows.

>Solution :

The correct UPDATE...FROM syntax for SQLite 3.33.0+ is:

UPDATE tableA AS a 
SET data = b.data 
FROM tableB AS b
WHERE a.pattern LIKE b.pattern || '%';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading