id cid content datetime
1 1 aaaa 1306314239
2 1 aaaa 1306314129
3 2 aaaa 1306314243
4 2 aaaa 1306314342
5 2 aaaa 1306314253要求
1:查询cid = 1 和cid = 2 中时间最大的一条?
2:查询datatime 时间少 一天 or 一小时的条数?
1 1 aaaa 1306314239
2 1 aaaa 1306314129
3 2 aaaa 1306314243
4 2 aaaa 1306314342
5 2 aaaa 1306314253要求
1:查询cid = 1 和cid = 2 中时间最大的一条?
2:查询datatime 时间少 一天 or 一小时的条数?
select * from table1 where cid = 1 or cid = 2 order by datetime desc limit 1;2:查询datatime 时间少 一天 or 一小时的条数?
select * from table1 order by datatime limit 1
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
select *
from tb A
where not exists (select 1 from tb A.cid=cid and A.cid<cid)
1:查询cid = 1 时间最大的一条和cid = 2 中时间最大的一条 共两条
2:查询 查询一天前的 或 一个小时前的记录数 回复二楼 由于问题没有说清 答案有差距
回复三楼 由于问题没有说清 答案有差距回复四楼 not exists 子查询 其实我没有看懂 希望解释一下 谢谢
select * from tt a where not exists(select 1 from tt where a.cid=cid and a.datetime<datetime)
select * from tt where date(FROM_UNIXTIME(datetime))=CURDATE()-INTERVAL 1 DAY
or
FROM_UNIXTIME(datetime)=now()-INTERVAL 1 hour
参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....