SELECT count(*),sum(summa),date,type FROM `table` WHERE STATUS = 'close' and type='buy' GROUP BY DAY(date) order by now() desc limit 7 

How to display the latest records by date? On desc/asc does not respond.

  • 2
    What do you mean by order by now() desc ? > how to display the latest records by date? For some specific period? - Opalosolo
  • > SELECT count (*), sum (summa), date > GROUP BY DAY ( date ) This is something that should be avoided. date is the name of the function, and in some cases, queries may not be executed or not performed as desired. - etki
  • @ ua6xh, the query displays the amount (sum (summa)) for each day. Order by now () displays everything in order, from the first date to the current day. when you insert limit 7, it should output the last 7 groups, and displays the first 7 groups - shogun
  • 2
    @shogun, order by now() sorts by the result of the now() function - the current date, which is right now. that is, it does not sort in any way. - etki

1 answer 1

 SELECT count(*), sum(summa), date, type FROM 'table' WHERE STATUS = 'close' and type='buy' GROUP BY DAY(date) order by date desc limit 7