MySQL从其他表中更新多行多个值,给出重复值

时间:2016-05-20 18:45:20

标签: mysql sql-update

我有一个简单的查询,如下所示

UPDATE tablea, 
       tableb 
SET    tablea.id = tableb.id 
WHERE  tablea.phone1 = tableb.phone2 

更新后我得到了意想不到的结果。请参阅http://hastebin.com/opibutixow.1c

在vicidial_list表格中使用custom_1026表格中的电话号码:

SELECT lead_id 
FROM   vicidial_list 
WHERE  phone_number IN (SELECT fos_mobile 
                        FROM   custom_1026); 
+---------+
| lead_id |
+---------+
|   79498 |
|   79499 |
|   79500 |
|   79497 |
|   79496 |
|   79495 |
|   79494 |
|   79493 |
|   79492 |
|   79491 |
|   79490 |
+---------+
更新命令后,

在custom_1026中导致重复

UPDATE custom_1026 
       INNER JOIN vicidial_list 
               ON ( custom_1026.fos_mobile = vicidial_list.phone_number ) 
SET    custom_1026.lead_id = vicidial_list.lead_id; 

SELECT lead_id FROM custom_1026;

+---------+
| lead_id |
+---------+
|   79500 |
|   79499 |
|   79492 |
|   79493 |
|   79495 |
|   79495 |
|   79498 |
|   79498 |
|   79498 |
|   79491 |
|   79491 |
+---------+

1 个答案:

答案 0 :(得分:0)

我想你想要

UPDATE tablea a
JOIN   tableb b
  ON   a.phone1 = b.phone2 
SET    a.id = b.id

但我认为你问的是错误的问题。

首先尝试检查您的数据是否正确,可能是您有重复的电话号码。

SELECT phone_number , count(*)
FROM vicidial_list 
GROUP BY phone_number 
HAVING COUNT(*) > 1

SELECT fos_mobile, count(*)
FROM custom_1026
GROUP BY fos_mobile 
HAVING COUNT(*) > 1