如题,比如:
用户a,时间,其它字段
用户b,时间,其它字段
用户a,时间,其它字段
用户c,时间,其它字段
想取每个用户,每天时间最晚的那条记录。表中时间格式,年月日小时分秒都有。
最后结果是
用户a,时间,其它字段
用户b,时间,其它字段
用户c,时间,其它字段 这个问题我在oracle里面请教过,但那个语法在mysql里面执行不了。谁给个mysql里面的写法。谢谢!
用户a,时间,其它字段
用户b,时间,其它字段
用户a,时间,其它字段
用户c,时间,其它字段
想取每个用户,每天时间最晚的那条记录。表中时间格式,年月日小时分秒都有。
最后结果是
用户a,时间,其它字段
用户b,时间,其它字段
用户c,时间,其它字段 这个问题我在oracle里面请教过,但那个语法在mysql里面执行不了。谁给个mysql里面的写法。谢谢!
解决方案 »
- c# 如何调用mysql的函数(有返回值的)
- mysql调用存储过程出现Illegal mix of collations错误
- 为什么我在MySQL 下新建的的用户不能创建自己的数据库?
- 用mysql数据库,有个表每天都会新增100--200w条数据
- mysql 的臨時表是物理表?
- 同时UPDATA多个表的问题.并且只使用一次UPDATA,请问怎么写这个语句,并且还分二种情况, 请高手指教??
- 两个host的问题。
- 要查出A表中B字段值最大的前十条记录如何写SQL啊?
- 4.1发布在即,庆祝!200分,来者有份!!!
- PhpMyadmin怎么样导入和导出数据?
- MySql root 密码是否有时效?
- 请帮忙看下这个查询排序语句。
from yourTable a
where not exists (select 1 from yourTable where 用户=a.用户 and 时间>a.时间)
[征集]分组取最大N条记录方法征集,...
from yourTable a
where not exists (select 1 from yourTable where 用户=a.用户 and date(时间)=date(a.时间) and 时间>a.时间)
建议提问时用例子来描述,这样可以避免理解上的偏差,反而需要更多的描述和沟通。
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
表中两个字段,
name_a,2009-12-01 11:10:20
name_b,2009-12-01 12:11:20
name_c,2009-12-01 20:10:20
name_a,2009-12-07 11:10:20
name_a,2009-12-07 19:10:20
执行后结果
name_a,2009-12-01 11:10:20
name_b,2009-12-01 12:11:20
name_c,2009-12-01 20:10:20
name_a,2009-12-07 19:10:20
如果第一个字段,在同一天有重复的,就把时间早的那条去掉,如果当天里没有重复就保留该记录
name_a,2009-12-01 11:10:20
name_b,2009-12-01 12:11:20
name_c,2009-12-01 20:10:20
name_a,2009-12-07 11:10:20
name_a,2009-12-07 19:10:20
三楼的答案,执行结果是
name_b,2009-12-01 12:11:20
name_c,2009-12-01 20:10:20
name_a,2009-12-07 19:10:20
而我想要的是
name_a,2009-12-01 11:10:20
name_b,2009-12-01 12:11:20
name_c,2009-12-01 20:10:20
name_a,2009-12-07 19:10:20
如果第一个字段,在同一天有重复的,就把时间早的那条去掉,如果当天里没有重复就保留该记录
and
(date_format(a.time,'%Y-%m-%d)=date_format(b.time,'%Y-%m-%d) and a.time>=b.time
)
group by
a.name,a.time having count(b.time)=1
and
(date_format(a.time,'%Y-%m-%d)=date_format(b.time,'%Y-%m-%d) and a.time>=b.time
)
group by
a.name,a.time having count(b.time)=1
mysql> select * from t_cashcow;
+--------+---------------------+
| name | td |
+--------+---------------------+
| name_a | 2009-12-01 11:10:20 |
| name_b | 2009-12-01 12:11:20 |
| name_c | 2009-12-01 20:10:20 |
| name_a | 2009-12-07 11:10:20 |
| name_a | 2009-12-07 19:10:20 |
+--------+---------------------+
5 rows in set (0.00 sec)mysql> select *
-> from t_cashcow a
-> where not exists (select 1 from t_cashcow where name=a.name and date(td)=date(a.td) and td>a.td);
+--------+---------------------+
| name | td |
+--------+---------------------+
| name_a | 2009-12-01 11:10:20 |
| name_b | 2009-12-01 12:11:20 |
| name_c | 2009-12-01 20:10:20 |
| name_a | 2009-12-07 19:10:20 |
+--------+---------------------+
4 rows in set (0.06 sec)mysql>