在 UPDATE 语句中使用 HAVING 子句
此查询
SELECT
FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, StatTypeId, COUNT(*) AS 'Count'
FROM NCAAstats
INNER JOIN College_Translator
ON College_Translator.AccountID = NCAAstats.AccountId
GROUP BY FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, CalendarYear, StatTypeId
HAVING COUNT(*) >1
ORDER BY 'Count' DESC
选择我想要将 ISValid
位设置为 0
的记录。
这些记录是由于输入错误而在我的数据库中出现两次的记录。
我正在寻找类似的东西:
UPDATE NCAAstats
SET IsValid = 0
WHERE (my select statement)
This is on MS SQL SERVER 2008
谢谢!
This query
SELECT
FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, StatTypeId, COUNT(*) AS 'Count'
FROM NCAAstats
INNER JOIN College_Translator
ON College_Translator.AccountID = NCAAstats.AccountId
GROUP BY FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, CalendarYear, StatTypeId
HAVING COUNT(*) >1
ORDER BY 'Count' DESC
Selects records that I would like to set an ISValid
bit to 0
.
These records are records that appear twice in my database due to an input error.
I'm looking for something like:
UPDATE NCAAstats
SET IsValid = 0
WHERE (my select statement)
This is on MS SQL SERVER 2008
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以像这样加入该子查询:
You can join to that subquery like so:
SQL Server 可以执行如下更新:
您应该首先查看此处:
http://msdn.microsoft.com/en-us/library/aa260662(v=sql.80).aspx
SQL Server can do updates like:
You should look here first:
http://msdn.microsoft.com/en-us/library/aa260662(v=sql.80).aspx
上面是很好的建议......这是另一种简单的方法:
** 如果我弄乱了列名称,请原谅我,但你明白了。
The above are good suggestions.... here's another easy way to do it :
** Forgive me if I messed up the columns name, but you get the idea.
使用 CTE,并执行基本上自连接的操作
,或者更好的是,使用窗口函数。
请注意,第二个不会将“第一”记录更新为无效,并且它假设 NCAAstats 和 College_Translator 之间存在 1 对 1 的关系。
Use a CTE, and do what is basically a self join
Or better yet, use the windowing functions.
Note that second does not update the "first" record to invalid, and that it assumes that there that there is a 1 to 1 relationship between NCAAstats and College_Translator.
对于 SQL Server 17
For SQL Server 17