表的结构是:
name    名字
ttime   时间
place   地点
word    单词
wgt    权重
total 总量
PRIMARY KEY  (`name`,`ttime`,`place`,`word`),我想查找,某个名字,例如“Tom”最近10天都有哪些“单词”。该怎么写?
一个人一天可能有很多个place,这时候只取total最大的那个作为当天的纪录。
例如
tom  2009-04-02 北京 吃饭 8 2344
tom  2009-04-02 北京 游玩 8 2344
tom  2009-04-02 上海 开会 9 1234那么,2009-04-02当天的纪录就是两条
tom  2009-04-02 北京 吃饭 8 2344
tom  2009-04-02 北京 游玩 8 2344
由此选取当天的单词就是 吃饭和游玩。。

解决方案 »

  1.   

    多几条记录,以供测试
    select a.* from tt a
    inner join
    (select name,max(total) as ma from tt where ttime='2009-04-02') b
    on a.name=b.name and a.total=b.ma
      

  2.   

    “Tom”最近10天都有哪些“单词”select word from yourTable where name='Tom' and ttime>DATE_SUB(CURDATE() ,INTERVAL 10 days);
      

  3.   

    最近10天:
    select a.* from tt a
    inner join
    (select name,max(total) as ma from tt where 
    ttime=DATE_ADD(curdate(),INTERVAL -10 day) b
    on a.name=b.name and a.total=b.ma
      

  4.   

    最近10天:
    select a.* from tt a
    inner join
    (select name,max(total) as ma from tt where 
    ttime=DATE_ADD(curdate(),INTERVAL -10 day) b
    on a.name=b.name and a.total=b.ma
      

  5.   

    一个人一天可能有很多个place,这时候只取total最大的那个作为当天的纪录。select *
    from yourTable a
    where name='Tom' and ttime>DATE_SUB(CURDATE() ,INTERVAL 10 days)
    and not exists (select 1 from yourTable where name=a.name and ttime=a.ttime and total>a.total);
      

  6.   

    select a.* from tt a
    inner join
    (select name,max(total) as ma from tt where
    ttime=DATE_ADD(curdate(),INTERVAL -10 day group by name) b
    on a.name=b.name and a.total=b.ma
      

  7.   

    当然也还有多种写法,比如用 join max()  具体可以参考
    分组取前N记录 http://blog.csdn.net/ACMAIN_CHM/archive/2009/04/26/4126306.aspx
    中的特例 N=1
      

  8.   

    说错了对不起各位。。描述错误。。
    应该是,选取最近有记录的10天,每天输出10个“单词”..
    例如数据是
    tom  2009-04-02 北京 吃饭 8 2344
    tom  2009-04-02 北京 游玩 8 2344
    tom  2009-04-02 上海 开会 9 1234 (这一条将被删除,因为都是2009-04-02的,而且 1234<2344 )
    tom  2009-02-23 天津 开会 2 3442
    tom  2009-02-23 天津 打假 2 3442
    tom  2009-02-23 天津 约会 2 3442那么选取tom最近有数据的十天,所有的记录应该是:tom  2009-04-02 北京 吃饭 8 2344
    tom  2009-04-02 北京 游玩 8 2344
    tom  2009-02-23 天津 开会 2 3442
    tom  2009-02-23 天津 打假 2 3442
    tom  2009-02-23 天津 约会 2 3442 (一样可以输出,因为单词数量目前是3,还没到10呢..)
      

  9.   

    还有HAVING 写法,因为你没有更多记录,无法测试
      

  10.   

    说错了对不起各位。。描述错误。。
    应该是,选取最近有记录的10天,每天输出10个“单词”..
    例如数据是
    tom  2009-04-02 北京 吃饭 8 2344
    tom  2009-04-02 北京 游玩 8 2344
    tom  2009-04-02 上海 开会 9 1234 (这一条将被删除,因为都是2009-04-02的,而且 1234 <2344 )
    tom  2009-02-23 天津 开会 2 3442
    tom  2009-02-23 天津 打假 2 3442
    tom  2009-02-23 天津 约会 2 3442那么选取tom最近有数据的十天,所有的记录应该是:tom  2009-04-02 北京 吃饭 8 2344
    tom  2009-04-02 北京 游玩 8 2344
    tom  2009-02-23 天津 开会 2 3442
    tom  2009-02-23 天津 打假 2 3442
    tom  2009-02-23 天津 约会 2 3442 (一样可以输出,因为单词数量目前是3,还没到10呢..) 
      

  11.   

    用你上述数据:select a.* from tt a left join
    (select name,ttime,place,max(total) as ma from tt group by name,ttime,place) b
    on 
    a.name=b.name and a.ttime=b.ttime and a.place=b.place 
    and a.total=b.ma
    limit 10
      

  12.   

    select a.* from tt a inner join
    (select name,ttime,place,max(total) as ma from tt group by name,ttime,place) b
    on
    a.name=b.name and a.ttime=b.ttime and a.place=b.place
    and a.total=b.ma
    order by name limit 10 
      

  13.   

    如果这样,用SQL语句实现不如用程序来实现了。1 得出十天内每天的 total, s1_date, s1_total
    select ttime,max(total)
    from yourTable a
    where name='Tom' and ttime>DATE_SUB(CURDATE() ,INTERVAL 10 days)2.然后再循环得到每天的十条记录.select *
    from yourTable
    where name='Tom' and ttime= s1_date and total=s1_total
    limit 10;这样效率比一个单独的SQL语句反而高。