There is a table

enter image description here If I make the following request

SELECT DISTINCT id_event FROM mes LEFT JOIN searchjur ON mes.id_event = searchjur.id WHERE to=58 ORDER BY t5ime_mes DESC LIMIT 20 

then my sample is as follows. First, it selects all the unique id_event values, and then sorts the result of this selection by the t5ime_mes field, and how to make the sample go down the t5ime_mes field, but take the unique values ​​of the id_event field? Tried to do

 SELECT `id`, `id_event`, max(`t5ime_mes`) FROM ( SELECT * FROM `mes` ORDER BY `t5ime_mes` DESC ) as `new_table` GROUP BY `id_event` 

but it also did not give a positive result

  • try using group by - Vladimir Klykov
  • The same result - Victor Vasiliev
  • one
    you need to rephrase your question a bit: "how to get the maximum t5ime_mes for each id_event" and then it will be easy to translate to sql :) select id_event, max(t5ime_mes) from .... group by id_event - Mike
  • I was delighted early (He sorts in ascending order id_event, but I need t5ime_mes - Victor Vasiliev
  • SELECT id_event , max ( time_mes ) FROM mes LEFT JOIN searchjur ON mes . id_event = searchjur . id WHERE to = 58 GROUP BY id_event DESC ORDER BY t5ime_mes - Victor Vasiliev

2 answers 2

There is a barbaric way to get what you want to see (almost like in a comment). select Id_event, mes_to, MAX (t5ime_to) from table1 group by id_event order by MAX (t5ime_to) desc; But I'm not sure that this will solve your problem. Example with MIN, table exp1

I can not add to the comments. (

     SELECT id_event FROM mes LEFT JOIN searchjur ON mes.id_event = searchjur.id WHERE to=58 GROUP BY id_event ORDER BY MAX(t5ime_mes) DESC LIMIT 20