3

我试图在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
4

3 回答 3

1

试试这个:

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

检查这个SQL FIDDLE 演示

输出

| 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 |
于 2013-01-16T07:03:26.500 回答
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
于 2013-01-16T06:57:52.140 回答
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  
于 2013-01-16T07:04:13.470 回答