【问题标题】:GET Rows WITH MIN(Date) on different column valuesGET Rows WITH MIN(Date) 在不同的列值上
【发布时间】:2013-06-05 09:23:09
【问题描述】:

我想问一个关于我觉得很难完成的查询的问题,至少对我来说是这样。我需要下面表格的行(用红色矩形标记),它们有min(date),而表格有不同的用户id's,quiz id's。

如果有人可以帮助我,我会很高兴!

提前致谢!

Here是一张来自表格的图片

【问题讨论】:

    标签: mysql select group-by


    【解决方案1】:

    试试这个 sql。如果您有多个用户进行多项考试,此 sql 将有所帮助。

    SQL Fiddle

    MySQL 5.5.30 架构设置

    create table test(
    userid int,
    enddate datetime,
    quizid int);
    
    insert into test
    values(823,'2013-02-06 14:23:05',5);
    
    insert into test
    values(823,'2013-02-06 14:23:05',5);
    
    insert into test
    values(823,'2013-02-06 14:23:05',5);
    
    
    insert into test
    values(824,'2013-02-06 14:23:08',5);
    
    
    insert into test
    values(824,'2013-02-06 14:23:10',5);
    

    查询 1

     select DISTINCT userid,quizid,enddate
        from test
        where enddate IN(
        select min(enddate)
        from test
        group by userId,quizId
        )
    

    Results

    | USERID | QUIZID |                         ENDDATE |
    -----------------------------------------------------
    |    823 |      5 | February, 06 2013 14:23:05+0000 |
    |    824 |      5 | February, 06 2013 14:23:08+0000 |
    

    【讨论】:

      【解决方案2】:
          select t1.* from sometable t1
          where t1.EndDate = (select min(t2.EndDate) from sometable t2)
      

      【讨论】:

        【解决方案3】:

        您应该能够进行简单的查询,例如:

        select * from your_table_name where EndDate = (select min(EndDate) from your_table_name)
        

        【讨论】:

          猜你喜欢
          • 2021-03-02
          • 1970-01-01
          • 1970-01-01
          • 2017-11-27
          • 1970-01-01
          • 1970-01-01
          • 2014-04-09
          • 2015-10-05
          • 1970-01-01
          相关资源
          最近更新 更多