SQL 在 UPDATE 语句中使用 HAVING 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8793914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a HAVING clause in an UPDATE statement
提问by Tyler DeWitt
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
.
选择我想设置ISValid
一点的记录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
这是在 MS SQL SERVER 2008 上
Thanks!
谢谢!
回答by Eric
You can join to that subquery like so:
您可以像这样加入该子查询:
update n1 set
isvalid = 0
from
ncaastats n1
inner join (
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
) n2 on
n1.accountid = n2.accountid
回答by Mithrandir
SQL Server can do updates like:
SQL Server 可以执行以下更新:
UPDATE table SET col=vaue
FROM (
SELECT ......
)
You should look here first:
你应该先看看这里:
http://msdn.microsoft.com/en-us/library/aa260662(v=sql.80).aspx
http://msdn.microsoft.com/en-us/library/aa260662(v=sql.80).aspx
回答by Segev -CJ- Shmueli
The above are good suggestions.... here's another easy way to do it :
以上是很好的建议......这是另一种简单的方法:
update ncaastats set isvalid = 0
where accountId in (
SELECT AccountId
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
)
** Forgive me if I messed up the columns name, but you get the idea.
** 如果我弄乱了列名,请原谅我,但你明白了。
回答by jmoreno
Use a CTE, and do what is basically a self join
使用 CTE,并执行基本上是自连接的操作
;with NCAAstatsToUpdate(
SELECT AccountId
FROM NCAAstats n
INNER JOIN College_Translator ct
ON ct.AccountID = n.AccountId
GROUP BY FirstName, LastName, n.AccountId, ct.school_name,
CalendarYear, StatTypeId
HAVING COUNT(*) >1 )
UPDATE NCAAstats
SET IsValid=0
FROM NCAAstats n
inner join NCAAstatsToUpdate u
on n.AccountId = u.AccountId
Or better yet, use the windowing functions.
或者更好的是,使用窗口函数。
;with NCStats as(
Select distinct row_number() over (partition by FirstName, LastName, n.AccountId, ct.school_name,
CalendarYear, StatTypeId order by n.accountId) rw, n.*
FROM NCAAstats n
INNER JOIN College_Translator ct
ON ct.AccountID = n.AccountId
)
Update NCStats
Set IsValid=0
Where rw>1
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.
请注意, second 不会将“第一”记录更新为无效,并且它假定 NCAAstats 和 College_Translator 之间存在一对一的关系。
回答by Quickee
For SQL Server 17
对于 SQL Server 17
UPDATE table SET col = val
(SELECT cols FROM table .. )