我有一个超过 100 万行的目标表。每次我都会得到 50K 行,其中可能包含多个重复的条目。因此,我决定将 CSV 数据存储到一个临时表中,然后通过比较两个表之间的行从 temp_table 到 target_table...
如果发现重复的条目,将数据从 temp_table 附加到 target_table,否则插入表中...我在这里使用分区,所以 ON DUPLICATE 键更新在这里不起作用..在 temp_table 中我没有使用任何 KEYS
我有两个表,如下所示
temp_table
Name | Type
John | Civil
John | Mech
target_table
Name | Type
John | Civil
当我运行下面的查询时,我得到单行的输出
UPDATE target_table JOIN temp_table
ON temp_table.Name = target_table.Name
SET target_table.Type = IF((LOCATE(temp_table.Type, target_table.Type) > 0)
target_table.Type,CONCAT(target_table.Type,',',temp_table.Type))
target_table
Name | Type
John | Civil
我希望输出如下所示
target_table
Name | Type
John | Civil, Mech
我可以知道哪里出了问题吗?
最佳答案
你应该使用 group_concat 并在连接中使用子查询
UPDATE target_table
JOIN (
select name, group_concat(Type) grouped
from temp_table
group by name
) t ON t.Name = target_table.Name
SET target_table.Type = t.grouped
关于php - UPDATE 使用 JOIN 仅将第一行值附加到另一个表,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52714638/