刪除SQL表中重複的資料

2021-06-21 02:56:06 字數 640 閱讀 8724



第一步:將表中的資料放到乙個臨時表裡面

select identity(int, 1, 1)  as id,

* into #temp

from   dianpingpoi  ***

where  ***.districtid = 4313 and ***.cuisineid = 210

第二步:刪除臨時表裡面的重複資料  

delete #temp

where  id not in (select max(id)

from   #temp

group by

所有該錶的列名)

第三步:刪除表裡面符合條件的資料                     

delete from  dianpingpoi  where districtid=4313 and cuisineid=116

第四步:將臨時表裡面的資料插入到表中

insert into dianpingpoi

(所有該錶的列名

)select        所有該錶的列名  from   #temp

第五步:刪除臨時表

drop table #temp



筆記 刪除表中重複資料的sql

主要是要理解group by 語句的作用。group by 語句用來對行資料進行分組,故,需要查詢的列必須都被包含在分組的列中,可少不可多。如下刪除表 t temp中與content列重複的資料 思路是先找出不需刪除的id,然後外面套用乙個not in 語句進行刪除。不需刪除的id,使用group ...

刪除表中重複的資料

原始表,和相關資料 drop table if existsstudent create tablestudent idint 11 not null auto increment,stunamevarchar 100 default null,idint 100 default null,prim...

刪除表中重複資料

刪除表中重複資料 取出line fancy2表中fancy name相同的最小fancy id 寫進tmp表 create table tmp as select min fancy id as col1 from line fancy2 group by fancy name 刪除line fan...