sql server 2005 '更新自'询问 [英] sql server 2005 'update from' query

查看:39
本文介绍了sql server 2005 '更新自'询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据另一个表中的用户 ID 更新一个表.我从语法中遇到了更新,但我正在努力正确编写我的查询.

I'm trying to update a table based upon the user id from another table. I've come across the Update from syntax but I'm struggling to write my query correctly.

下面的代码应该向您展示我正在尝试做什么.当我运行它时,我得到 0 行影响.

The below code should show you what I'm attempting to do. When i run it i get 0 rows affected.

    update jared_test
       set user_count  = 1
      from new_user nuj
inner join (select us.userID
              from users us
             where us.email = 'j@j.co.uk') u on nuj.userid = u.userid

/********编辑*******************\

/********EDIT*******************\

我发现我的 Cursor 循环存在问题,导致它无法正常工作,因此这确实有效.但是,如果在这种情况下 where 比 from 更好进行优化,我会很感兴趣.

I discovered there was a problem with my Cursor loop that was preventing this from working, so this does actually work. However I'd be interested if a where is better than a from in this instance for optimisations.

推荐答案

我不是 100% 解释为什么其他解决方案使用子选择,它的执行速度通常比常规连接慢.虽然 taos subselect 本质上是一个普通的 join,只是写的很有趣.

I'm not 100% on why the other solutions are using a subselect which will perform slower than a regular join most often. Though taos subselect is essentially a regular join just written interestingly.

update aliasName
set aliasName.user_count =1 
from new_user aliasName
inner join users u on aliasName.userid = u.userid
where email = 'j@j.co.uk'

这篇关于sql server 2005 '更新自'询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆