zoukankan      html  css  js  c++  java
  • 如何根据日期字段查询表中最新的数据

    需求分析: 有时候我们有这样的需求,需要根据一个表中的某一字段进行分类,但是我们有想在分类后拿到分类后最新的记录,如果直接用

    group by来获取会发生sql错误

      例如 这里有张学生表studentInfo,有字段: id(学号),name(姓名),age(年龄),class(班级),reportDate(报名时间)

         我们需要查出每个班级最新报名的学生信息

         错误的sql:   select id,name,age,class,max(reportDate) from studentInfo group by class;

         group by子句只能让我们查询类别分类后共有的属性,而个人的具体信息是不允许查询的

         我们应该借助子查询,先将我们每个班最新的报名时间查询出来,再作为日期的条件查询主表

         select * from studentInfo where reportDate in (select max(reportDate) from studentInfo group by reportDate);

            

  • 相关阅读:
    怎样用HTML5 Canvas制作一个简单的游戏
    js面向对象
    javascript闭包
    javascript变量的作用域
    5-22
    5-23
    14-5-21 硬代码
    14-5-19 类和对象
    array
    生成干扰线
  • 原文地址:https://www.cnblogs.com/gangbalei/p/6670392.html
Copyright © 2011-2022 走看看