表 A 用户
Id Int 主键
Name varchar(20)
regDate datetime
表 B 上传信息
Id int 主键
UId int 外键与a关联
FileName varchar(20) ..
sql 实现:
1。最近三天注册的用户中上传文件数量最多的用户及上传数量
2。删除用户表A中用户名重复的最后一条(注册时间晚的)--------
:没有‘上传数量’等其他任何字段谢谢. 

解决方案 »

  1.   


    1:   select  top 1 s.name,count(t.FileName ) from B t ,
         (select  id,name from A where datediff(dd,getdtae(),regDate)<=3) s
         where t.uid=s.id
          group by s.name
          order by count(1) desc
      

  2.   


    1
    select  top 1 s.name,count(t.FileName ) from B t ,
         (select  id,name from A where datediff(dd,getdtae(),regDate)<=3) s
         where t.uid=s.id
          group by s.name
          order by count(t.FileName ) desc2  delete  s
       from a s,(select name,max(regDate) as regDate from a group by name) t
       where a.name=t.name and
             a.regDate=t.regDate 
      

  3.   

    第2个 修改一下别名delete  s
       from a s,(select name,max(regDate) as regDate from a group by name) t
       where s.name=t.name and
             s.regDate=t.regDate 
      

  4.   


    1:select top 1 name, count(filename) 
    from a join b on a.id=b.uid 
    where datediff(day,regDate ,getdate())<3 
    group by a.id order by count(filename) desc 
     
    2:delete   A1  from a A1  where not exists(select  1 from a where   name=A1.name and regDate<A1.regDate )
      

  5.   

    --1、
    select top 1 name,num=count(*) from A ,B
    where a.id=b.uid and datediff(day,regDate,getdate())<=3
    group by name 
    order by count(*)
    --2、
    delete from a
    where not exists (select * from a b where b.name=a.name and b.regDate>regDate)
      

  6.   


    --建表,只建表A(我懒...)
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[A]
    GOCREATE TABLE [dbo].[A] (
    [Id] [int] IDENTITY (1, 1) NOT NULL ,
    [Name] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [RegDate] [smalldatetime] NOT NULL 
    ) ON [PRIMARY]
    GO--插入数据,用户dd不存在重复的记录,删除时不应将其删除
    INSERT INTO [A]
    SELECT 'aa',getdate()
    UNION 
    SELECT 'aa',dateadd(day,-1,getdate())
    UNION
    SELECT 'aa',dateadd(day,-4,getdate())
    UNION
    SELECT 'aa',dateadd(day,-7,getdate())
    UNION
    SELECT 'bb',getdate()
    UNION 
    SELECT 'bb',dateadd(day,-1,getdate())
    UNION
    SELECT 'bb',dateadd(day,-4,getdate())
    UNION
    SELECT 'cc',getdate()
    UNION 
    SELECT 'cc',dateadd(day,-1,getdate())
    UNION
    SELECT 'dd',getdate()
    GO--1
    SELECT TOP 1 [Name],COUNT(*) FROM A
    JOIN B ON A.Id = B.Uid
    WHERE datediff(day,regDate,getdate())<=3
    GROUP BY Name 
    ORDER BY COUNT(*)--2
    DELETE FROM t1
    FROM A t1
    WHERE t1.regDate = ( 
      SELECT MAX(regDate) FROM A t2
      WHERE t1.Name = t2.Name
      GROUP BY [Name]
      HAVING COUNT(*) > 1
    )
    删除的时候注意外键约束,或者可以将外键设置为ON DELETE CASCADE 
      

  7.   

    我在 9楼 对问题2的答案应该没问题问题1还需要再考虑一下,因为用户名是可以重复的,GROUP BY NAME似乎不对还有个问题,不好意思,我怎么没找到“编辑帖子”的链接啊,是不是发表之后就不能编辑了啊??
      

  8.   

    我分别测试了一下
    返现  ks_reny 的第一个 我这边差不多成了
    du209 的第二个 差不多成了。
      

  9.   

    datediff(dd,getdtae(),regDate) <= 3 使用的不对吧
    应该是datediff(dd,regDate,getdtae()) <= 3DATEDIFF ( datepart , startdate , enddate ) 是 enddate - startdate
    注册时间(regDate)肯定是小于当前时间的,如果是datediff(dd,getdtae(),regDate)的话,返回的结果是非正数,是肯定小于3的正确地筛选最近三天注册的用户应该是用 datediff(dd,regDate,getdtae()) <= 3
    使用SELECT *, DATEDIFF(dd, GETDATE(), RegDate) AS 天数差 FROM A 测试一下就知道了