从Select结果更新我的数据库

时间:2012-03-28 19:38:06

标签: mysql sql database

mysql> SELECT Ext, Pass, Name, Context FROM temp_Users WHERE temp_Users.Pass NOT IN (SELECT Pass FROM Users);
+------+-------+---------+------------+
| Ext  | Pass  | Name    | Context    |
+------+-------+---------+------------+
| 6003 | Hello | WebPone | DLPN_Admin |
+------+-------+---------+------------+
1 row in set (0.00 sec)


mysql> UPDATE Users
    -> SET (Pass, Name, Context) = (SELECT  Pass, Name, Context FROM temp_Users WHERE temp_Users.Pass NOT IN (SELECT Pass FROM Users))
    -> WHERE Users.Ext = temp.Ext;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Pass, Name, Context) = (SELECT  Pass, Name, Context FROM temp_Users WHERE temp_' at line 2

我想从Select结果更新我的数据库,我收到此错误。请告诉我如何解决它?

2 个答案:

答案 0 :(得分:3)

MySQL不支持SET ( multiple_fields ) = ( subquery_that_returns_multiple_fields )语句的UPDATE语法。相反,您必须使用“多表”更新(连接)。请参阅http://dev.mysql.com/doc/refman/5.6/en/update.html

您的查询还有一些其他问题,所以我不清楚完全您想要什么。 。 。但我想你想要这样的东西:

UPDATE users
  JOIN temp_users
    ON temp_users.ext = users.ext
   SET users.pass = temp_users.pass,
       users.name = temp_users.name,
       users.context = temp_users.context
 WHERE temp_users.pass NOT IN
        -- extra subquery to bypass MySQL limitation:
        ( SELECT pass
            FROM ( SELECT pass
                     FROM users
                 ) t
        )
;

答案 1 :(得分:2)

UPDATE Users u
JOIN temp_Users tu ON tu.Ext = u.Ext
SET 
    Pass = tu.Pass,
    Name = tu.Name,
    Context = tu.Context
WHERE tu.Pass NOT IN (
    SELECT Pass
    FROM Users
)