使用嵌套查询进行更新

时间:2012-06-15 15:35:13

标签: mysql

以下是我的查询和相关错误。我做错了什么?

update aaalogin 
  set name = 
      ( select aaacontactinfo.emailid 
        from aaacontactinfo 
        WHERE aaalogin.user_id = aaausercontactinfo.user_id 
          and aaausercontactinfo.contactinfo_id = aaacontactinfo.contactinfo_id
      );

我得到了

  

错误:'where子句'中的未知列'aaausercontactinfo.user_id'

2 个答案:

答案 0 :(得分:0)

aaausercontactinfo未包含在FROM表格列表中,请尝试使用:

UPDATE aaalogin SET name =
(
    SELECT aaacontactinfo.emailid
    FROM aaacontactinfo, aaalogin, aaausercontactinfo
    WHERE aaalogin.user_id = aaausercontactinfo.user_id
    AND aaausercontactinfo.contactinfo_id = aaacontactinfo.contactinfo_id
);

答案 1 :(得分:0)

UPDATE aaalogin SET name =
(
    SELECT aaacontactinfo.emailid
    FROM aaacontactinfo, aaausercontactinfo
    WHERE aaalogin.user_id = aaausercontactinfo.user_id
    AND aaausercontactinfo.contactinfo_id = aaacontactinfo.contactinfo_id
);

子查询中不需要aaalogin。如果使用它甚至可以在子查询中返回多个记录。