2

任何人都可以帮助处理这个 sql 代码,我正在尝试将第一个匹配的表列 FirstURLForSite 更新为 1,但我正在努力处理代码,我只想更新第一个匹配。

任何人都可以帮助,希望你能理解我想要做什么。

update [dbo].[at_WebsitesSearch]
set [FirstURLForSite] = 1
where(

left outer join
select min(ID) as rowid, [BaseURL]
from [dbo].[at_WebsitesSearch]
group by [BaseURL]) as FirstMatch on
[dbo].[at_WebsitesSearch].id = FirstMatch.rowid
where FirstMatch.rowid is null

数据示例

BasesURL                                FirstURLForSite
http://asia1x1.net/                         1
http://asia1x1.net/                         0
http://asia1x1.net/                         0

http://australia.bedandbreakfasts.net/      1
http://australia.bedandbreakfasts.net/      0
http://australia.bedandbreakfasts.net/      0
4

2 回答 2

3

试试看,我正在尝试修复您当前的查询

UPDATE  a
SET     a.FirstURLForSite = 1
FROM    at_WebsitesSearch a
        LEFT JOIN 
        (
            SELECT  MIN(ID) AS rowid, BaseURL
            FROM    at_WebsitesSearch
            GROUP   BY BaseURL
        ) FirstMatch
            ON FirstMatch.rowid = a.id
WHERE   FirstMatch.rowid IS NULL

更新 1

WITH records
AS
(
    SELECT  BasesURL, FirstURLForSite,
            ROW_NUMBER() OVER (PARTITION BY BasesURL ORDER BY BasesURL ASC) rn
    FROM    at_WebsitesSearch
)
UPDATE  records
SET     FirstURLForSite = CASE WHEN rn = 1 THEN rn ELSE 0 END
于 2013-03-02T13:35:42.930 回答
0

你必须做这样的事情。还要检查下面的 Fiddle Demo。

;WITH CTE  
AS  
(  
    SELECT  BasesURL, FirstURLForSite,  
        ROW_NUMBER() OVER (PARTITION BY BasesURL   
                            ORDER BY BasesURL DESC) RN  
    FROM    at_WebsitesSearch  
)  
UPDATE  CTE  
SET     FirstURLForSite = 1  
WHERE RN = 1  

SQL 小提琴演示

于 2013-03-02T13:58:06.240 回答