{"id":2381,"date":"2010-05-04T11:38:38","date_gmt":"2010-05-04T09:38:38","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=2381"},"modified":"2010-05-04T11:39:00","modified_gmt":"2010-05-04T09:39:00","slug":"discovery-of-the-day-group-by-desc","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/discovery-of-the-day-group-by-desc","title":{"rendered":"Discovery of the day: GROUP BY &#8230; DESC"},"content":{"rendered":"<p>I happened on a query where, by mistake, an<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT ... ORDER BY x DESC LIMIT 1\r\n<\/pre>\n<p>was written as<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT ... GROUP BY x DESC LIMIT 1\r\n<\/pre>\n<p>And it took me by surprise to realize <strong>GROUP BY x DESC<\/strong> is a valid statement. I looked it up: yep! It&#8217;s <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/group-by-modifiers.html\">documented<\/a>.<\/p>\n<p>In MySQL, <strong>GROUP BY<\/strong> results are sorted according to the group statement. You can override this by adding <strong>ORDER BY NULL<\/strong> (see <a href=\"http:\/\/code.openark.org\/blog\/mysql\/less-known-sql-syntax-and-functions-in-mysql\">past post<\/a>). I wasn&#8217;t aware you can actually control the sort order.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I happened on a query where, by mistake, an SELECT &#8230; ORDER BY x DESC LIMIT 1 was written as SELECT &#8230; GROUP BY x DESC LIMIT 1 And it took me by surprise to realize GROUP BY x DESC is a valid statement. I looked it up: yep! It&#8217;s documented. In MySQL, GROUP BY [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[21,20],"class_list":["post-2381","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-sql","tag-syntax"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-Cp","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/2381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=2381"}],"version-history":[{"count":4,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/2381\/revisions"}],"predecessor-version":[{"id":2385,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/2381\/revisions\/2385"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=2381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=2381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=2381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}