表的结构是:
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
由此选取当天的单词就是 吃饭和游玩。。
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
由此选取当天的单词就是 吃饭和游玩。。
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
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
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
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);
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
分组取前N记录 http://blog.csdn.net/ACMAIN_CHM/archive/2009/04/26/4126306.aspx
中的特例 N=1
应该是,选取最近有记录的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呢..)
应该是,选取最近有记录的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呢..)
(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
(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
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语句反而高。