Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL删除循环_Sql_Loops_Count - Fatal编程技术网

SQL删除循环

SQL删除循环,sql,loops,count,Sql,Loops,Count,我有一张房屋清单。我想保持每个城市最多10个列表。 大多数城市只有不到10个房源 执行此查询时: select city, count(city) as cityCount from tREaltyTrac group by city SQL返回: Acampo 1 Acton 1 Adelanto 20 Agua Dulce 1 Aguanga 1 Akron 19 Albany 12 Albion 3 Alexandria 14 Algonac 1 Alhambr

我有一张房屋清单。我想保持每个城市最多10个列表。 大多数城市只有不到10个房源

执行此查询时:

 select city, count(city) as cityCount from tREaltyTrac group by city
SQL返回:

Acampo  1
Acton   1
Adelanto    20
Agua Dulce  1
Aguanga 1
Akron   19
Albany  12
Albion  3
Alexandria  14
Algonac 1
Alhambra    5
因此,阿德兰托、阿克伦、奥尔巴尼和亚历山大必须缩减到只有10个列表,这些列表是按照最新的日期时间字段“创建”顺序按创建描述排序的


有人能想到一种方法,可以像前面所描述的那样减少列表吗?

你不应该从数据库中删除记录。 这不是100%的完美,我相信有更好的方法来做到这一点,但你来了

declare @cities (CityName nvarchar(50), ID int identity(1,1))
declare @returns (CityName nvarchar(50), Blah nvarchar(50))
declare @cityname nvarchar(50)
declare @count int
declare @i int


insert into @cities (CityName)
select distinct CityName
from tblCities

select @count = count(*) from @cities
set @i=1

while (@i<=@count)
begin

    select @cityname = CityName from @cities where ID=@i

    select top 10 *
    from tblCities
    where CityName=@cityname
    order by Creation desc

    set @i=@i+1
end

我现在没有时间写出实际的代码,但是像这样的东西怎么样。。。。假设PHP

订购初始查询-添加创建说明 循环遍历结果,并获得第10个结果的创建日期 运行另一个查询,删除第10个结果的创建值以下的所有内容?-i、 e.删除创建<$CreationDateOfthResult的位置
希望这是有意义的…

我会选择前十名,将它们放入临时表中,删除原始记录并用临时表填充。类似的事情应该会为您解决,但自动删除记录不是一个好主意。您最好使用活动标志

DECLARE @CityName VARCHAR(30)
DECLARE CitiesOver10 CURSOR FOR select city from tREaltyTrac group by city having count(city)>10 

OPEN CitiesOver10
FETCH NEXT FROM CitiesOver10 INTO @CityName

WHILE @@FETCH_STATUS = 0
    BEGIN
        DELETE FROM 
            dbo.tREaltyTrac 
        WHERE 
            ID NOT IN (SELECT TOP 10 ID FROM dbo.tREaltyTrac WHERE city = @CityName ORDER BY Creation DESC)
            AND City = @CityName

        FETCH NEXT FROM CitiesOver10 INTO @CityName
    END

CLOSE CitiesOver10
DEALLOCATE CitiesOver10

取决于您的sql版本。类似的方法可能会奏效

选择r.城市,b* 来自tREaltyTrac r 在中的b.Id上加入tREaltyTrac b 从tREaltyTrac中选择前10个Id,其中Id=r。Id按Id顺序描述 按r.City分组

不要循环

我更喜欢用状态标记第行,但这将根据您的需要删除它们

试试这个sql server

摆桌子

create table tREaltyTrac (city varchar(20),creation datetime)
insert into tREaltyTrac values ('Acampo'      ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Acton'       ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Agua Dulce'  ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Aguanga'     ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albion'      ,getdate()) --3
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albion'      ,getdate()) --3
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albion'      ,getdate()) --3
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Algonac'     ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
显示表格值

select city,count(*) from tREaltyTrac group by city
select * from tREaltyTrac
删除不需要的行

delete from tREaltyTrac
    from tREaltyTrac
        inner join (select
                        city,creation, row_number() over(partition by city order by city) AS RankValue
                        from tREaltyTrac
                   ) dt on tREaltyTrac.city=dt.city AND tREaltyTrac.creation=dt.creation
    where dt. RankValue>10
显示剩下的行

select * from tREaltyTrac
select city,count(*) from tREaltyTrac group by city

你想根据什么标准来挑选城市?我保留了creation desc最新10.RDBMS风格和版本订购的前10名。你能发布表格定义吗?它有主键吗?你用的是哪台服务器?什么版本?另一个可能的解决方案。。。不使用日期,而是使用一个唯一的ID字段,然后您的删除查询可以是delete,其中uniqueID<$uniqueIDOfTenthResult.PHP?我希望您不要获取所有行并在PHP中循环它们,然后从中删除。循环非常慢,有很多方法可以在一个SQL语句中实现,请参阅我的答案1-避免像瘟疫一样的游标!有更聪明的方法可以做到这一点。光标循环非常慢,通常有方法使用单个语句,请看我的回答循环非常慢,有方法可以在单个语句中实现这一点,请看我的回答是的,我想有更好的方法来实现它,当时我想不起来。这就是为什么我把它作为我评论的一部分。插入操作每次延迟1秒,因此时间会有所不同,这样你就可以知道最早的插入是被删除的插入了!在其他性能较差的SQL引擎中,您可能会被建议为SQL Server显然处理得很好的内容创建一个临时表,以及一个嵌套的select,不幸的是,并非所有引擎都支持SQL'03标准的行数过度分配,叹气。+1用于使用行数排序函数!我用同样的方法在两台CTE上工作,但你击败了我:-