{"id":507,"date":"2012-03-23T15:56:11","date_gmt":"2012-03-23T13:56:11","guid":{"rendered":"https:\/\/polyetilen.lt\/?p=507"},"modified":"2023-05-02T09:22:14","modified_gmt":"2023-05-02T06:22:14","slug":"sort-by-specific-order-in-mysql-query","status":"publish","type":"post","link":"https:\/\/polyetilen.lt\/en\/sort-by-specific-order-in-mysql-query","title":{"rendered":"Sort by specific order in MySQL query"},"content":{"rendered":"<p>If you want to sort the list by a certain order, such as a list of numbers from the beginning of 4,5,1, and then in ascending order, you can create a sort order by IF statement rule with additional columns for sorting:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT id,\r\n-- additional columns for sorting\r\nIF(id=4, 1, 0) c1, IF(id=5, 1, 0) c2, IF(id=1, 1, 0) c3 \r\nFROM test \r\nWHERE id IN(1,2,3,4,5,6,7,8,9,10)\r\n-- now sort by additional columns descending and other ascending\r\nORDER BY c1 DESC, c2 DESC, c3 DESC, id ASC\r\n<\/pre>\n<p>Query result:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nid\tc1\tc2\tc3\r\n4\t1\t0\t0\r\n5\t0\t1\t0\r\n1\t0\t0\t1\r\n6\t0\t0\t0\r\n7\t0\t0\t0\r\n8\t0\t0\t0\r\n9\t0\t0\t0\r\n10\t0\t0\t0\r\n<\/pre>\n<p>In this example, the statement condition returns 1 or 0, since 0 is above 1, so do a reverse sort by descending order, then the values \u200b\u200bin additional columns will be first. MySQL statements can be used immediately in the sorting, then the query is changed as follows:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT id \r\nFROM test \r\nWHERE id IN(1,2,3,4,5,6,7,8,9,10)\r\n-- sort order by conditions\r\nORDER BY IF(id=4, 1, 0) DESC, IF(id=5, 1, 0) DESC, IF(id=1, 1, 0) DESC, id ASC\r\n<\/pre>\n<p>Query result:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nid\r\n4\r\n5\r\n1\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<p>Since the IF statement return 0 or 1, it can be replaced by equal operator, so query would be:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT id \r\nFROM test \r\nWHERE id IN(1,2,3,4,5,6,7,8,9,10)\r\n-- sort order by equal operators\r\nORDER BY id=4 DESC, id=5 DESC, id=1 DESC, id ASC\r\n<\/pre>\n<p>Query result:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nid\r\n4\r\n5\r\n1\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<p>Instead IF statement can use CASE statement:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT id\r\nFROM test \r\nWHERE id IN(1,2,3,4,5,6,7,8,9,10)\r\nORDER BY \r\n-- sort by case\r\nCASE id\r\n    WHEN 4 THEN 1\r\n    WHEN 5 THEN 2\r\n    WHEN 1 THEN 3\r\n    ELSE 4\r\nEND ASC\r\n<\/pre>\n<p>Query result:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nid\r\n4\r\n5\r\n1\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to sort the list by a certain order, such as a list of numbers from the beginning of 4,5,1, and then in ascending order, you can create a sort order by IF &hellip; <a href=\"https:\/\/polyetilen.lt\/en\/sort-by-specific-order-in-mysql-query\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"http:\/\/polyetilen.lt\/?p=272","footnotes":""},"categories":[8],"tags":[32,105,107,106],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-programavimas","tag-mysql","tag-rusiavimas","tag-rusiavimo-tvarka","tag-uzklausa","en-US"],"_links":{"self":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":1,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":508,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/507\/revisions\/508"}],"wp:attachment":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}