1

I noticed by accident that this doesn't seem to be a syntax error like I would have expected

select `InvoiceNumber`, `_Total`, date(`__Added`)`Date` 
from `orders`
where`__Added`>=curdate()-interval 7 day
group by `Date`desc
order by `Date`;

Removing it doesn't appear to change my results, and desc doesn't appear anywhere on the doc page for group by https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html

Is it just thrown out by the parser? Or does using group by desc actually do something?

1
  • desc and asc are for ordering the results. Not sure if they have any relationship with group by.
    – Allen King
    Aug 5, 2019 at 21:50

2 Answers 2

6

from the docs

Prior to MySQL 8.0.13, MySQL supported a nonstandard syntax extension that permitted explicit ASC or DESC designators for GROUP BY columns.

It probably stems from MySQL GROUP BY also implied ordering as well; at least did at one time, they stated a while back the feature was deprecated, but I am not sure if has actually been removed yet.

2
  • Ah that's why it wasn't doing anything in my case; because I also had an order by, that makes sense Aug 5, 2019 at 22:04
  • And yes, on my 8.0 instance it is actually a syntax error Aug 5, 2019 at 22:06
1

Try removing the order by, you'll see that the result set is ordered by the group asc/desc direction.

Of course this is bad practice, don't expect it to be portable across DBMS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.