我怎样才能在mysql中获得最高分数

时间:2013-01-16 06:57:08

标签: mysql sql select sql-order-by

我想在mysql中获得最高分数。

当我运行此查询时: -

select tid from post order by marks desc limit 0,10;

我得到的是最高分,但不是日期。

当我运行此查询时。

select tid from post order by date desc limit 0,10.

我首先获得价值日期...... 但我希望获得最高分的日期......

例如我的表是: -

s.no    marks   date
1       97      15-01-2013
2       104     14-01-2013
3       100     16-01-2013
4       105     16-01-2013

所以,我想得到

s.no    marks   date
4       105     16-01-2013
3       100     16-01-2013
1       97      15-01-2013
2       104     14-01-2013

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT tid, marks, date 
FROM post 
ORDER BY date DESC, marks DESC 
LIMIT 0,10; 

选中此SQL FIDDLE DEMO

输出

| ID | MARKS |                           DATE |
-----------------------------------------------
|  4 |   105 | January, 16 2013 00:00:00+0000 |
|  3 |   100 | January, 16 2013 00:00:00+0000 |
|  1 |    97 | January, 15 2013 00:00:00+0000 |
|  2 |   104 | January, 14 2013 00:00:00+0000 |

答案 1 :(得分:0)

要获得问题中显示的结果,您只需要先按日期排序表,然后按标记排序:

SELECT * FROM post ORDER BY date DESC, marks DESC LIMIT 10

但是,如果您想要每个日期的最大分数,则需要在日期分组表格,选择每个组的最大分数:

SELECT date, MAX(marks) AS marks FROM post GROUP BY date LIMIT 10

如果您想从最大记录中获取其他数据,例如tid,则必须将此查询的结果加回到您的表中:

SELECT post.* FROM post NATURAL JOIN (
  SELECT date, MAX(marks) AS marks FROM post GROUP BY date LIMIT 10
) t

答案 2 :(得分:0)

SELECT 
        post.date,
        p.Marks
FROM post 
INNER JOIN (
            SELECT 
                MAX(marks) as Marks , 
                date , 
                s.no 
            FROM posts 
            GROUP BY s.no
            )  as p ON p.date = post.date
ORDER BY post.date