php - UPDATE 使用 JOIN 仅将第一行值附加到另一个表,为什么?

标签 php mysql phpmyadmin

我有一个超过 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/

相关文章:

php - 来自 mysql 的 IDs 来自同时来源的大量插入

MySQL唯一索引设计

phpmyadmin - 如何访问 PhpMyAdmin 中的书签查询?

php - 无法访问本地网络上的 wamp server 2.5 服务

mysql - CSV 日期格式转换为 MySQL 日期格式

php - 如何转义 php 和 mysql 问题的句点?

PHPMailer 无法连接 ot 服务器

javascript - PHP中带标签select的操作

MySql-从三个表中选择不同的值

php - 无法连接到 PostgreSQL 服务器