博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

--更新数据
1.推荐: update #t1 set #t1.c2 = #t2.c2, #t1.c3 = #t2.c3,
 #t1.c4 = #t2.c4
from #t2
where #t1.c1 = #t2.c1

 

2.

update  tags   set  usednumber=usednumber+(select b.usednumber
from  tag_tousenumber b
where tags.tagname=b.tagname and tags.channelid=b.channelid and datediff(day,b.addeddate,getdate())<1)

会导致第一个表中有第二个表中没有的数据变成null

3.

update a set a.usednumber=a.usednumber+b.usednumber  from tags    a
     left join tag_tousenumber  b
     on a.tagname=b.tagname     

同样会导致第一个表中有第二个表中没有的数据变成null

 

 

 

 

删除一个表的重复记录

 

delete bbs_sort where id in (SELECT DISTINCT a.id FROM bbs_sort AS a,bbs_sort AS b WHERE a.sign=b.sign AND a.sort_name=b.sort_name AND a.rank=b.rank AND a.id<b.id)

字段完全重复的

select distinct * into #Tmp from tableName
    drop table tableName
    select * into tableName from #Tmp
    drop table #Tmp

 

获取一个表(有区县商圈)区县的列表并按照原来的排序出结果

select MIN(id) id, district  from rent_xxx where city='北京' group by district  order by id asc