Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL double ORDER BY

I got a MySQL tablw which will store the contest participant informations.

Structure like

id   +   user_id    +   marks     +   date_added
-----+--------------+-------------+----------------------
1    |   24         |    30       |  2014-02-06 03:04:08
-----+--------------+-------------+----------------------
1    |   25         |    10       |  2014-02-06 13:04:08
-----+--------------+-------------+----------------------
1    |   26         |    14       |  2014-02-06 05:04:08
-----+--------------+-------------+----------------------
1    |   27         |    54       |  2014-02-06 21:04:08

From this I need to find out a winner .

ie the winner with maximum marks. In case of multiple candidate with max marks then it will be on first come basis.

Got many contest with thousands of datas

Can anyone please show me a hint ?

Thanks in advance

like image 588
ramesh Avatar asked Jan 24 '26 00:01

ramesh


1 Answers

SELECT
    user_id
FROM
    `table name`
ORDER BY
    marks DESC, date_added ASC
LIMIT
    1

Replace table name though.

like image 70
Cthulhu Avatar answered Jan 25 '26 18:01

Cthulhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!