{"id":38,"date":"2006-08-23T14:30:08","date_gmt":"2006-08-23T14:30:08","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=38"},"modified":"2006-09-01T14:15:12","modified_gmt":"2006-09-01T14:15:12","slug":"order-by-puzzle","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=38","title":{"rendered":"Order By Puzzle"},"content":{"rendered":"<p>From a question on Freenode.<\/p>\n<pre>SELECT * FROM theTable;<\/pre>\n<pre>+----+----------+\r\n| id | data     |\r\n+----+----------+\r\n|  1 | middle   |\r\n|  2 | first    |\r\n|  3 | showLast |\r\n+----+----------+<\/pre>\n<p>Fair enough. Let&#8217;s order it by `data`.<\/p>\n<pre>SELECT * FROM theTable ORDER BY data;<\/pre>\n<pre>+----+----------+\r\n| id | data     |\r\n+----+----------+\r\n|  3 | showLast |\r\n|  1 | middle   |\r\n|  2 | first    |\r\n+----+----------+<\/pre>\n<p>What??? Maybe the server didn&#8217;t quite understand.<\/p>\n<pre>SELECT * FROM theTable ORDER BY data ASC;<\/pre>\n<pre>+----+----------+\r\n| id | data     |\r\n+----+----------+\r\n|  3 | showLast |\r\n|  1 | middle   |\r\n|  2 | first    |\r\n+----+----------+<\/pre>\n<p>How did that happen? I assure you I have not switched to some &#8216;reverse order&#8217; collation.<\/p>\n<p><spoiler 'Show Answer' 'Hide Answer'><\/p>\n<pre>SHOW CREATE TABLE theTable;<\/pre>\n<pre>CREATE TABLE `thetable` (\r\n  `id` int(11),\r\n  `data` enum('showLast','middle','first')\r\n)<\/pre>\n<p>Ah. `data` is an enum. ORDER BY on an enum orders by the enum index, not the string. You can order by CONCAT(data) or by CAST(data AS char). Are there better ways?<\/p>\n<p><\/spoiler><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From a question on Freenode. SELECT * FROM theTable; +&#8212;-+&#8212;&#8212;&#8212;-+ | id | data | +&#8212;-+&#8212;&#8212;&#8212;-+ | 1 | middle | | 2 | first | | 3 | showLast | +&#8212;-+&#8212;&#8212;&#8212;-+ Fair enough. Let&#8217;s order it by `data`. SELECT * FROM theTable ORDER BY data; +&#8212;-+&#8212;&#8212;&#8212;-+ | id | data | +&#8212;-+&#8212;&#8212;&#8212;-+ | 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_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,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[6],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-gotchas"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-C","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38"}],"version-history":[{"count":0,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}