2012-08-28 204 views
1
Timezones 
--------- 
-AreaCode varchar 
-Timezone varchar 

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

一切除了填充在联系人表Timezone,所以我想查找每个电话号码的时区和更新的联系人。这就是我试图这样做,但MySQLSQL更新,子查询返回多行

错误1242子查询返回不止一行

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

update contacts 
set contacts.timezone = '-1' 
where left(contacts.phone,3) = (Select timezones.areacode 
           from timezones 
           where timezones.timezone = '-1'); 
+2

唔,你已经验证了,真的是* *仅适用于每个ID对应一个时区? –

+0

我刚刚检查过,时区表 – user1630799

+0

中没有重复的区域码我询问重复的“时区”值,而不是重复的区域码。 –

回答

2

Y我们的子查询返回多行。只需更换“=”和“IN”来处理这个问题:

update contacts 
set contacts.timezone = '-1' 
where left(contacts.phone,3) in (Select timezones.areacode 
           from timezones 
           where timezones.timezone = '-1'); 
+0

太棒了!谢谢 – user1630799

0

尝试在更新内连接或子查询:

update contacts 
set contacts.timezone = '-1' 
where left(contacts.phone,3) in (Select timezones.areacode 
           from timezones 
           where timezones.timezone = '-1'); 
0

更改其中的一部分,如: ....

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

的问题是,可能有超过1行中具有时区牛逼imezone column = '-1'

您可以使用Join这里

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

它将匹配区域码与手机,并在这种情况下将更新与'-1'