{"id":178,"date":"2014-02-18T10:33:04","date_gmt":"2014-02-18T15:33:04","guid":{"rendered":"http:\/\/signaturewebmedia.com\/blog\/?p=178"},"modified":"2014-02-18T10:33:04","modified_gmt":"2014-02-18T15:33:04","slug":"mysql-order-by-date-type-issue","status":"publish","type":"post","link":"http:\/\/signaturewebmedia.com\/blog\/178\/mysql-order-by-date-type-issue\/","title":{"rendered":"MySQL Order By date type issue"},"content":{"rendered":"<p><a href=\"http:\/\/getfreeipad.us\/info\/offer1\/\" target=\"_blank\" rel=\"nofollow\"><img src=\"http:\/\/getfreeipad.us\/images\/ipad-tester-wanted.jpg\" border=\"0\"><\/a><\/p><p>This must be the &#8220;gotcha&#8221; type of stuff when you think just using the &#8220;ORDER BY &lt;date type&gt;&#8221; clause would give you back the correct results in MySQL.<\/p>\n<p>My original table structure and its data:<\/p>\n<pre>mysql&gt; describe holidays;\r\n+-----------------+--------------+------+-----+---------+----------------+\r\n| Field           | Type         | Null | Key | Default | Extra          |\r\n+-----------------+--------------+------+-----+---------+----------------+\r\n| holidayId       | int(11)      | NO   | PRI | NULL    | auto_increment |\r\n| holidayName     | varchar(255) | NO   |     | NULL    |                |\r\n| holidayDate     | date         | NO   |     | NULL    |                |\r\n| holidayDuration | int(11)      | NO   |     | NULL    |                |\r\n+-----------------+--------------+------+-----+---------+----------------+<\/pre>\n<pre>mysql&gt; select holidayId, holidayName, holidayDate from holidays;\r\n+-----------+-------------------+-------------+\r\n| holidayId | holidayName       | holidayDate |\r\n+-----------+-------------------+-------------+\r\n| 1         | Memorial Day      | 2014-05-26  |\r\n| 2         | Independence Day  | 2014-07-04  |\r\n| 3         | Labor Day         | 2014-09-01  |\r\n| 4         | Thanks Giving Day | 2014-11-27  |\r\n| 5         | Christmas Day     | 2014-12-25  |\r\n| 6         | New Year's Day    | 2015-01-01  |\r\n+-----------+-------------------+-------------+<\/pre>\n<p>Then I&#8217;d query the table with the ORDER BY clause with some DATE_FORMAT():<\/p>\n<pre>mysql&gt; SELECT holidayId, holidayName, date_format(holidayDate,'%m\/%d\/%Y') holidayDate\r\n -&gt; FROM holidays\r\n -&gt; ORDER BY holidayDate;\r\n+-----------+-------------------+-------------+\r\n| holidayId | holidayName       | holidayDate |\r\n+-----------+-------------------+-------------+\r\n| <strong><span style=\"color: #ff0000;\">6<\/span><\/strong>         | <span style=\"color: #ff0000;\"><strong>New Year's Day<\/strong><\/span>    | <strong><span style=\"color: #ff0000;\">01\/01\/2015<\/span><\/strong>  |\r\n| 1         | Memorial Day      | 05\/26\/2014  |\r\n| 2         | Independence Day  | 07\/04\/2014  |\r\n| 3         | Labor Day         | 09\/01\/2014  |\r\n| 4         | Thanks Giving Day | 11\/27\/2014  |\r\n| 5         | Christmas Day     | 12\/25\/2014  |\r\n+-----------+-------------------+-------------+<\/pre>\n<p>As you can see, the New Year&#8217;s Day for 2015 is at top. It seems like the records were sorted based on the numbers and not by the chronological orders.<\/p>\n<p>According to the manual (http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/date-and-time-functions.html#function_date-format), the DATE_FORMAT() would return &#8220;a string.&#8221;<\/p>\n<p>Thus, the correct way to sort the date type column is to sort by the original column name. In our case, the original column name was aliased\/renamed as &#8220;holidayDate.&#8221; If we did not alias this, it would have been:<\/p>\n<pre>mysql&gt; SELECT date_format(holidayDate,'%m\/%d\/%Y')\r\n -&gt; FROM holidays;\r\n+-------------------------------------+\r\n| <strong><span style=\"color: #ff0000;\">date_format(holidayDate,'%m\/%d\/%Y')<\/span><\/strong> |\r\n+-------------------------------------+\r\n| 05\/26\/2014                          |\r\n| 07\/04\/2014                          |\r\n| 09\/01\/2014                          |\r\n| 11\/27\/2014                          |\r\n| 12\/25\/2014                          |\r\n| 01\/01\/2015                          |\r\n+-------------------------------------+<\/pre>\n<pre>===&gt; it became \"date_format(holidayDate,'%m\/%d\/%Y')\"<\/pre>\n<p>Thus my workaround is (I want to use alias!):<\/p>\n<pre>mysql&gt; SELECT holidayId, holidayName, date_format(holidayDate,'%m\/%d\/%Y') <span style=\"color: #ff0000;\">'Holiday Date'<\/span>\r\n -&gt; FROM holidays\r\n -&gt; ORDER BY <strong><span style=\"color: #ff0000;\">holidayDate<\/span><\/strong>;\r\n+-----------+-------------------+--------------+\r\n| holidayId | holidayName       | Holiday Date |\r\n+-----------+-------------------+--------------+\r\n|         1 | Memorial Day      | 05\/26\/2014   |\r\n|         2 | Independence Day  | 07\/04\/2014   |\r\n|         3 | Labor Day         | 09\/01\/2014   |\r\n|         4 | Thanks Giving Day | 11\/27\/2014   |\r\n|         5 | Christmas Day     | 12\/25\/2014   |\r\n|         6 | New Year's Day    | 01\/01\/2015   |\r\n+-----------+-------------------+--------------+<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This must be the &#8220;gotcha&#8221; type of stuff when you think just using the &#8220;ORDER BY &lt;date type&gt;&#8221; clause would give you back the correct results in MySQL.<br \/>\nMy original table structure and its data:<br \/>\nmysql&gt; describe holidays;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| Field           | Type         | Null | Key | Default | Extra          |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| holidayId       | int(11)      | NO   | PRI | NULL    | auto_increment |<br \/>\n| holidayName     | varchar(255) | NO   |     | NULL    |                |<br \/>\n| holidayDate     | date         | NO   |     | NULL    |                |<br \/>\n| holidayDuration | int(11)      | NO   |     | NULL    |                |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\nmysql&gt; select holidayId, holidayName, holidayDate from holidays;<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| holidayId | holidayName       | holidayDate |<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| 1         | Memorial Day      | 2014-05-26  |<br \/>\n| 2         | Independence Day  | 2014-07-04  |<br \/>\n| 3         | Labor Day         | 2014-09-01  |<br \/>\n| 4         | Thanks Giving Day | 2014-11-27  |<br \/>\n| 5         | Christmas Day     | 2014-12-25  |<br \/>\n| 6         | New Year&#8217;s Day    | 2015-01-01  |<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+<br \/>\nThen I&#8217;d query the table with the ORDER BY clause with some DATE_FORMAT():<br \/>\nmysql&gt; SELECT holidayId, holidayName, date_format(holidayDate,&#8217;%m\/%d\/%Y&#8217;) holidayDate<br \/>\n -&gt; FROM holidays<br \/>\n -&gt; ORDER BY holidayDate;<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| holidayId | holidayName       | holidayDate |<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| 6         | New Year&#8217;s Day    | 01\/01\/2015  |<br \/>\n| 1         | Memorial Day      | 05\/26\/2014  |<br \/>\n| 2         | Independence Day  | 07\/04\/2014  |<br \/>\n| 3         | Labor Day         | 09\/01\/2014  |<br \/>\n| 4         | Thanks Giving Day | 11\/27\/2014  |<br \/>\n| 5         | Christmas Day     | 12\/25\/2014  |<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+<br \/>\nAs you can see, the New Year&#8217;s Day for 2015 is at top. It seems like the records were sorted based on the numbers and not by the chronological orders.<\/p>\n<p>According to the manual (http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/date-and-time-functions.html#function_date-format), the DATE_FORMAT() would return &#8220;a string.&#8221;<br \/>\nThus, the correct way to sort the date type column is to sort by the original column name. In our case, the original column name was aliased\/renamed as &#8220;holidayDate.&#8221; If we did not alias this, it would have been:<br \/>\nmysql&gt; SELECT date_format(holidayDate,&#8217;%m\/%d\/%Y&#8217;)<br \/>\n -&gt; FROM holidays;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| date_format(holidayDate,&#8217;%m\/%d\/%Y&#8217;) |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| 05\/26\/2014                          |<br \/>\n| 07\/04\/2014                          |<br \/>\n| 09\/01\/2014                          |<br \/>\n| 11\/27\/2014                          |<br \/>\n| 12\/25\/2014                          |<br \/>\n| 01\/01\/2015                          |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n===&gt; it became &#8220;date_format(holidayDate,&#8217;%m\/%d\/%Y&#8217;)&#8221;<br \/>\nThus my workaround is (I want to use alias!):<br \/>\nmysql&gt; SELECT holidayId, holidayName, date_format(holidayDate,&#8217;%m\/%d\/%Y&#8217;) &#8216;Holiday Date&#8217;<br \/>\n -&gt; FROM holidays<br \/>\n -&gt; ORDER BY holidayDate;<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n| holidayId | holidayName       | Holiday Date |<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n|         1 | Memorial Day      | 05\/26\/2014   |<br \/>\n|         2 | Independence Day  | 07\/04\/2014   |<br \/>\n|         3 | Labor Day         | 09\/01\/2014   |<br \/>\n|         4 | Thanks Giving Day | 11\/27\/2014   |<br \/>\n|         5 | Christmas Day     | 12\/25\/2014   |<br \/>\n|         6 | New Year&#8217;s Day    | 01\/01\/2015   |<br \/>\n+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/getfreeipad.us\/info\/offer2\/\" target=\"_blank\" rel=\"nofollow\">\r\n<IMG SRC=\"http:\/\/www.mb01.com\/getimage.asp?m=1753&o=2883&i=39158.dat\" width=300 height=250 border=0>\r\n<\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[21],"tags":[175,190,185,192,180,178,188,186,193,191,187,189,181,183,179,176,182,177],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts\/178"}],"collection":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":7,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":185,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions\/185"}],"wp:attachment":[{"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/signaturewebmedia.com\/blog\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}