【发布时间】:2021-10-24 16:07:48
【问题描述】:
我有两个表,并想使用 LIKE 语句使用在 table2(映射表)中找到的数据更新 table1(原始数据表)。 但是,我总是收到我所有不同尝试的错误消息:
查询错误:UPDATE/MERGE 必须最多匹配每个源行 目标行
Table1(数据表)
textWithFoundItemInIt | foundItem
---------------------------------
hallo Adam |
Bert says hello |
Want to find "Caesar"bdjehg |
Table2(映射表)
mappingItem
------------
Adam
Bert
Caesar
预期结果
textWithFoundItemInIt | foundItem
---------------------------------
hallo Adam | Adam
Bert says hello | Bert
Want to find "Caesar"bdjehg | Caesar
查询:
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, '%');
我还删除了表 1 和表 2 中的所有重复值,但仍然出现相同的错误消息。我也尝试使用 join 语句,但我得到了这个错误消息:“FROM 子句中的别名 table1 已定义为 UPDATE 目标”
我在 SO 中发现了这些类似的问题,并尝试使用他们的方法:
- 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?
不幸的是,他们对解决我的问题没有帮助。所以我认为这不是一个重复的问题。
非常感谢您的想法。
跟进问题
我指的是@Jon 发布的解决方案。再次感谢您的帮助。但是,在用不同的数据进行测试后,仍然存在如果'table1'中有重复则它不起作用的问题。 当然这个问题来自'GROUP BY'语句 - 如果没有这个,UPDATE查询不起作用,导致我原来的问题中所述的错误消息。如果我对每个值进行 GROUP,它也不起作用。
但是,我的“table1”(数据)和映射表“table2”中可能有重复项。所以说得非常准确,这是我的目标:
Table1(数据表)
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(映射表)
mappingItem
------------
Adam
Bert
Caesar
Bert
Caesar
Adam
预期结果
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
从 Table2 中找到哪个 Adam 并将其插入 Table1 并不重要,它们都是相同的。因此,如果第一个 Adam 将被第二个 Adam 覆盖,或者一旦找到一个 Adam,查询就停止进一步搜索。
如果我执行 Jon 的 'SELECT' 查询,它会导致:
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
它(正确地)省略了第二个“想再次找到“凯撒”,但不幸的是,这不是我需要的。
如果更容易的话,如果在一行中找到两个名字也可以
textWithFoundItemInIt | foundItem
---------------------------------------------
hallo Adam and Bert | Adam, Bert
Bert says hello to Caesar | Bert, Caesar
或
textWithFoundItemInIt | foundItem1 | foundItem2
---------------------------------------------------------------
hallo Adam and Bert | Adam | Bert
Bert says hello to Caesar | Bert | Caesar
我希望这有助于理解我的问题。简而言之:“这只是具有多个相等行的映射”;-)
非常感谢:)
【问题讨论】:
标签: sql google-bigquery mapping sql-like