0

我只是想知道如果 table1 和 table2 中的几个值相等,如何更新 table1 中的行。

例如, table1 有行:

id - password - attemp
user  secret       0
user2  pass        0

和 table2 有行"

id -  password
user   secret
user2   kek

将 table1 attemp 值(其中 table1 用户和密码相等且 table1 attemp = 0)更新为 table1 attemp = 1

我做了这样的事情(但它不起作用):

UPDATE a1
set a1.attemp = '1'
from table1 a1
JOIN table2 a2
on a1.user = a2.user AND a1.password = a2.password
WHERE 
a1.attemp != 1 
4

1 回答 1

0

试试这个:

UPDATE table1 a1
INNER JOIN table2 a2 ON a1.id = a2.id AND a1.password = a2.password
SET a1.attemp = '1'
WHERE a1.attemp != '1'

这将导致:

ID  PASSWORD    ATTEMP
user    secret  1
user2   pass    0

sqlfiddle demo

于 2013-11-14T14:56:23.357 回答