SQL: get maximum value and it's corresponding field(s) -
i need max lesson_score following table, along respective date particular user:
-------------------------------- |uid |lesson_score |date | -------------------------------- |1 |2 |1391023460 | |1 |8 |1391023518 | |1 |4 |1391023596 | --------------------------------
i need result of:
--------------------------- |lesson_score |date | --------------------------- |8 |1391023596 | ---------------------------
my sql looks this:
select date, max(lesson_score) lesson_score cdu_user_session_progress uid = 1 group date";
but gives me 3 rows:
--------------------------- |lesson_score |date | --------------------------- |2 |1391023460 | |4 |1391023596 | |8 |1391023518 | ---------------------------
what doing wrong? thanks!
try using
select lesson_score, date cdu_user_session_progress order lesson_score desc limit 1;
the order by
- part responsible, max. lession_score fetched @ beginning.
after order-by, folling result:
--------------------------- |lesson_score |date | --------------------------- |8 |1391023518 | |4 |1391023596 | |2 |1391023460 | ---------------------------
now limit
-part says, database should return first row - other result-rows ignored, , result this:
--------------------------- |lesson_score |date | --------------------------- |8 |1391023518 | ---------------------------
in order minium-score, write asc
intead of desc
(or remove it, because asc
default-value)
Comments
Post a Comment