I have two tables and want to update table1 (original data table) with data found in table2 (mapping table) using a LIKE statement. However, I always get the error message for all my different tries:
Query error: UPDATE/MERGE must match at most one source row for each target row
Table1 (data table)
textWithFoundItemInIt | foundItem
---------------------------------
hallo Adam |
Bert says hello |
Want to find "Caesar"bdjehg |
Table2 (mapping table)
mappingItem
------------
Adam
Bert
Caesar
Expected result
textWithFoundItemInIt | foundItem
---------------------------------
hallo Adam | Adam
Bert says hello | Bert
Want to find "Caesar"bdjehg | Caesar
Queries:
UPDATE `table1`
SET foundItem= mt.mappingItem
FROM `mappingTable` mt
WHERE textWithFoundItemInIt LIKE CONCAT('%', mt.mappingItem, '%');
UPDATE `table1`
SET foundItem= mt.mappingItem
FROM `mappingTable` mt
WHERE INSTR(textWithFoundItemInIt , mt.mappingItem) >1;
UPDATE `table1`
SET foundItem = (SELECT mt.mappingItem FROM `table2` AS mt
WHERE textWithFoundItemInIt LIKE CONCAT('%', mt.mappingItem, '%')
)
WHERE TRUE;
UPDATE `table1`
SET foundItem= mt.mappingItem
FROM `table1`
inner join `table2` mt on textWithFoundItemInIt LIKE CONCAT('%', mt.mappingItem, '%');
I have also removed all duplicate values from both table 1 and table 2, but still same error message. I also tried to use a join statement, but I got this err msg: "Alias table1 in the FROM clause was already defined as the UPDATE target"
I found these similar questions in SO and tried to use their approaches:
- update columns values with column of another table based on condition
- Using one table's values to query another table in BigQuery
- SQL update from one Table to another based on a ID match
- How to efficiently select records matching substring in another table using BigQuery?
Unfortunately they were not helpful to solve my problem. So I think this is not a duplicate question.
Thank you very much for your thoughts.
FOLLOW UP QUESTION
I am referring to the solution posted by @Jon. Once again thank you for your help. However, after testing it with different data, there is still the issue that it does not work if there are duplicates in 'table1'. Of course this problem comes from the 'GROUP BY' statement - and w/o this, the UPDATE query does not work, resulting in the error message stated in my original question. It doesn't work either, if I GROUP every value.
However, there can be duplicates in my 'table1' (Data) and my mapping table 'table2'. So to make it very precise, this is my goal:
Table1 (data table)
textWithFoundItemInIt | foundItem
-------------------------------------------
hallo Adam |
Bert says hello |
Bert says byebye |
Want to find "Caesar"bdjehg |
Want to find "Caesar"bdjehg |
Want to find "Caesar"again |
Want to find "CaesarCaesar"again and again | <== This is no problem, just finding one Caesar is enough
Table2 (mapping table)
mappingItem
------------
Adam
Bert
Caesar
Bert
Caesar
Adam
Expected result
textWithFoundItemInIt | foundItem
--------------------------------------------
hallo Adam | Adam
Bert says hello | Bert
Bert says byebye | Bert
Want to find "Caesar"bdjehg | Caesar
Want to find "Caesar"bdjehg | Caesar
Want to find "Caesar"again | Caesar
Want to find "CaesarCaesar"again and again | Caesar
It doesn't matter which Adam from Table2 is found and inserted into Table1, they will be the same. So it is even okay if the first Adam will be over written by the second Adam, or if the query just stops to search any further once one Adam is found.
If I execute Jon's 'SELECT' query, it would result in:
textWithFoundItemInIt | foundItem
--------------------------------------------
hallo Adam | Adam
Bert says hello | Bert
Bert says byebye | Bert
Want to find "Caesar"bdjehg | Caesar
Want to find "Caesar"again | Caesar
Want to find "CaesarCaesar"again and again | Caesar
It (correctly) omits the second "Want to find "Caesar"again", but that's unfortunately not what I need.
If it is easier, it would also be okay that in cases that two names are found in one row
textWithFoundItemInIt | foundItem
---------------------------------------------
hallo Adam and Bert | Adam, Bert
Bert says hello to Caesar | Bert, Caesar
or
textWithFoundItemInIt | foundItem1 | foundItem2
---------------------------------------------------------------
hallo Adam and Bert | Adam | Bert
Bert says hello to Caesar | Bert | Caesar
I hope this helps to understand my issue. In easy words: "It's just a mapping with multiple equal rows" ;-)
Thanks a lot :)