Mysql SQL更新,子查询返回多行

Mysql SQL更新,子查询返回多行,mysql,subquery,sql-update,Mysql,Subquery,Sql Update,除了联系人表中的Timezone,所有内容都已填充,因此我想查找每个电话号码的时区并更新联系人。下面是我试图做的,但是MySQL给出了 错误1242子查询返回多行 对于每个时区(0、-1、-2、-3、-4、-5),我执行以下更新: Timezones --------- -AreaCode varchar -Timezone varchar Contacts -------- -Phone varchar -Timezone varchar 尝试更新的内部联接或子查询: update

除了联系人表中的
Timezone
,所有内容都已填充,因此我想查找每个电话号码的时区并更新联系人。下面是我试图做的,但是
MySQL
给出了

错误1242子查询返回多行

对于每个时区(0、-1、-2、-3、-4、-5),我执行以下更新:

Timezones
---------
-AreaCode varchar
-Timezone varchar

Contacts
--------
-Phone    varchar
-Timezone varchar

尝试更新的内部联接或子查询:

update contacts 
set contacts.timezone = '-1' 
where left(contacts.phone,3) = (Select timezones.areacode 
                                from timezones 
                                where timezones.timezone = '-1');
更改零件的位置,如:


问题是,时区中可能有多行具有t
imezone column='-1'

您可以在此处使用
Join

where left(...) in (select ...... ) 

它会将区号与手机匹配,在这种情况下,会更新为
'-1'

您的子查询返回多行。只需将“=”替换为“IN”即可处理此问题:

update contacts join timezones on left(contacts.phone,3) = timezones.areacode and timezones.timezone = '-1'
set contacts.timezone = '-1';

嗯,你验证过每个ID只有一个时区吗?我刚刚检查过,时区表中没有重复的区号我问的是重复的
时区
值,而不是重复的区号。
update contacts join timezones on left(contacts.phone,3) = timezones.areacode and timezones.timezone = '-1'
set contacts.timezone = '-1';
update contacts 
set contacts.timezone = '-1' 
where left(contacts.phone,3) in (Select timezones.areacode 
                                from timezones 
                                where timezones.timezone = '-1');