zoukankan      html  css  js  c++  java
  • [转]Mysql将数据分组后取出时间最近的数据

    如题,我在网上也找过相关解决方法,很多解答都是这么一句SQL语句:
    select Id,AccountId,Mark,max(CreateTime) as Latest from AccountMark as b group by AccountId 

    使用Max函数。但是在我查出来的数据中似乎有些不对,如图,反白的那一条数据,Mark字段和CreateTime字段根本不对应啊!



    这是怎么回事?使用Max函数后在分组这样靠谱吗?

    还有一条语句:select *,COUNT(AccountId) as Num from 
    (select * from AccountMark order by CreateTime desc) `temp`  
    group by AccountId order by CreateTime desc 
    这样查出来的数据是对的


    但是,我需要创建视图,Mysql中视图里不允许出现查询子句。求大神些一条SQL语句,能实现既不出现子句,又能查出正确数据。谢谢!

    解决方法

    select * from AccountMark as b where not exists(select 1 from AccountMark where AccountId= b.AccountId
    and b.CreateTime<CreateTime )

    原文地址:http://bbs.csdn.net/topics/390958705

  • 相关阅读:
    uva-10160-枚举
    zk-systemd
    c++官方文档-枚举-联合体-结构体-typedef-using
    c++官方文档-动态内存
    c++官方文档-指针
    c++官方文档-命名空间
    c++官方文档-模版函数和重载
    c++官方文档-按值传递和按引用传递
    c++官方文档
    HDU 1068
  • 原文地址:https://www.cnblogs.com/o0oo/p/6907905.html
Copyright © 2011-2022 走看看