请你帮我改一下下面的查询(我知道它很简单,但无法弄清楚)。
update a
set a.password='sam'
from family_header a,
family_member b
where
a.id=b.match_key and
a.username='sam' and
a.pin_code='600061' and
b.dob='1948-10-12'
a.name=b.name;
我有来自family_header的用户名,密码,来自family_member的dob以及匹配密钥作为id,两个表中都有match_key和name。再次感谢!
答案 0 :(得分:0)
必须是这样的:
UPDATE family_header
SET password = 'sam'
WHERE EXISTS (SELECT 1
FROM family_member
WHERE family_header.id = family_member.match_key
AND family_header.name = family_member.name
AND family_member.dob = '1948-10-22')
AND pin_code = '600061'
AND username = 'sam')
答案 1 :(得分:0)
使用内部联接:
UPDATE a
SET a.PASSWORD = 'sam'
FROM family_header a
INNER JOIN family_member b ON a.id = b.Match_key
WHERE a.username = 'sam' AND
a.pin_code = '600061' AND
b.dob = '1948-10-12' AND
a.NAME = b.NAME
但我想你可能不需要“a.Name = b.Name”,因为你已经有了match_key ......
答案 2 :(得分:0)
更新语法必须是UPDATE,你不能使用别名而且没有“From” - 它不是select。 Checkout http://dev.mysql.com/doc/refman/5.0/en/update.html(这适用于MySQL,但在更新和选择时,T-SQL非常通用)
假设你正在使用MySQL,你也在update子句中使用连接:
update family_header a
inner join family_member b
on a.id = b.match_key
set a.password='sam'
where a.username='sam'
and a.pin_code='600061'
and b.dob='1948-10-12'
你不需要 a.name = b.name - * a.id = b.match_key *就足够了。
答案 3 :(得分:0)
将更新与联接
一起使用 update a
set a.[password]='sam1'
from #family_header a
inner join #family_member b on
a.id=b.match_key and
a.name=b.name WHERE a.username='sam' and
a.pin_code='600061' and
b.dob='1948-10-12'