从子查询更新查询

时间:2015-02-20 17:38:20

标签: sql-server subquery

我想看看是否有人可以帮我处理更新查询。

我需要抓住latitude&来自两个longitude中的同一用户的accounts。因此,我试图在一个帐户的用户表中update lat / long,基于另一个帐户中的同一用户。我通过电话号码和姓氏匹配用户。我尝试了subquery,但遇到了困难。

我在考虑以下内容:

update users
set lat = getlat, long = getlong
inner join (select lat as getlat, long as get long from users where account_id = '1')
on phone = phone and last name = lastname
where account_id = '2' and lat IS NULL and long IS NULL

我正朝着正确的方向前进吗?

提前感谢您的任何帮助!!!!

1 个答案:

答案 0 :(得分:0)

Alias

需要sub-query个名称

同时选择phone中的lastnamesub-query列,将其加入Users

update U
set U.lat = A.getlat, U.long = A.getlong
From users U
inner join 
(
select lat as getlat, long as getlong,phone ,lastname
from users 
where account_id = '1'
) A
on U.phone = A.phone and U.lastname = A.lastname
where account_id = '2' 
and lat IS NULL 
and long IS NULL