{"id":1163,"date":"2012-09-16T17:27:28","date_gmt":"2012-09-17T00:27:28","guid":{"rendered":"http:\/\/www.virendrachandak.com\/techtalk\/\/?p=1163"},"modified":"2013-10-03T11:13:21","modified_gmt":"2013-10-03T18:13:21","slug":"mysql-ordering-results-by-specific-field-values","status":"publish","type":"post","link":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/","title":{"rendered":"MySQL ordering results by specific field values"},"content":{"rendered":"<div>In MySQL we can sort the results in ascending or descending order very easily by using the <strong>ORDER BY<\/strong> clause. However, there are times when you want to sort the results in a specific order which cannot be done using the ASC or DSC. <strong>FIELD()<\/strong> of MySQL ORDER BY clause can be used to sort the results in a specific order.<\/div>\n<div>For this post I will use my <a href=\"http:\/\/www.virendrachandak.com\/techtalk\/\/sample-mysql-table\/\" title=\"Sample MySQL table\" target=\"_blank\">example MySQL table and data<\/a>. This is just a sample table with sample data, used to represent what I need. So, in this table I have different species of pets and I need to get a list of all pets in the order dogs, cat, snake and bird. I cannot use the MySQL ORDER BY clause to fetch the results in this order. By using the FIELD() function of the MySQL ORDER BY clause I can get the results in the order I need. Here is the query I can use.<\/div>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT name, species FROM `pet`\r\nORDER BY FIELD(species, 'dog','cat','snake','bird'), name ASC\r\n<\/pre>\n<p><!--more--><br \/>\nThis query would return me the results ordered by the species in the order I need and then sort it by name. Here is the result:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<th>name<\/th>\n<th>species<\/th>\n<\/tr>\n<tr>\n<td>Bowser<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Buffy<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Fang<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Claws<\/td>\n<td>cat<\/td>\n<\/tr>\n<tr>\n<td>Fluffy<\/td>\n<td>cat<\/td>\n<\/tr>\n<tr>\n<td>Slim<\/td>\n<td>snake<\/td>\n<\/tr>\n<tr>\n<td>Chirpy<\/td>\n<td>bird<\/td>\n<\/tr>\n<tr>\n<td>Whistler<\/td>\n<td>bird<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>The above solution works fine as along as there are no other values in the table for species apart from what specified in the FIELD function. In case there are other values and you need them to appear last in the list then we will need to change the query. Using the same table, consider that we want to sort the results by dog and cat first and anything else after then we can use the following query:<\/div>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT name, species FROM `pet` \r\nORDER BY FIELD(species, 'cat','dog') DESC , name ASC\r\n<\/pre>\n<div>\nThis will give us the following results:<\/p>\n<table>\n<tbody>\n<tr>\n<th>name<\/th>\n<th>species<\/th>\n<\/tr>\n<tr>\n<td>Bowser<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Buffy<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Fang<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Claws<\/td>\n<td>cat<\/td>\n<\/tr>\n<tr>\n<td>Fluffy<\/td>\n<td>cat<\/td>\n<\/tr>\n<tr>\n<td>Chirpy<\/td>\n<td>bird<\/td>\n<\/tr>\n<tr>\n<td>Slim<\/td>\n<td>snake<\/td>\n<\/tr>\n<tr>\n<td>Whistler<\/td>\n<td>bird<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\nNotice that the results are sorted by dog and cat, but not by any other species. If we need to have the other results also in group we can use the following query:<\/div>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT name, species FROM `pet` \r\nORDER BY FIELD(species, 'cat','dog') DESC, species ASC, name ASC\r\n<\/pre>\n<div>\nNow this query returns us the following results:<\/p>\n<table>\n<tbody>\n<tr>\n<th>name<\/th>\n<th>species<\/th>\n<\/tr>\n<tr>\n<td>Bowser<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Buffy<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Fang<\/td>\n<td>dog<\/td>\n<\/tr>\n<tr>\n<td>Claws<\/td>\n<td>cat<\/td>\n<\/tr>\n<tr>\n<td>Fluffy<\/td>\n<td>cat<\/td>\n<\/tr>\n<tr>\n<td>Chirpy<\/td>\n<td>bird<\/td>\n<\/tr>\n<tr>\n<td>Whistler<\/td>\n<td>bird<\/td>\n<\/tr>\n<tr>\n<td>Slim<\/td>\n<td>snake<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div><span style=\"text-decoration:underline;\">Note<\/span>: I searched the MySQL documentation but I am unable to find the proper documentation for this function. However, I have tried this on MySQL 5.0, 5.1, 5.5 and it works in all of them.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort the results in a specific order which cannot be done using the ASC or DSC. FIELD() of MySQL ORDER BY clause can be used to sort [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"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,"font":"","enabled":false},"version":2}},"categories":[48],"tags":[103,104,105],"class_list":["post-1163","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mysql-order-by","tag-mysql-results-order","tag-order-by-field"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL ordering results by specific field values - Virendra&#039;s TechTalk<\/title>\n<meta name=\"description\" content=\"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL ordering results by specific field values - Virendra&#039;s TechTalk\" \/>\n<meta property=\"og:description\" content=\"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\" \/>\n<meta property=\"og:site_name\" content=\"Virendra&#039;s TechTalk\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-17T00:27:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-10-03T18:13:21+00:00\" \/>\n<meta name=\"author\" content=\"Virendra Chandak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:site\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Chandak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\"},\"author\":{\"name\":\"Virendra Chandak\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\"},\"headline\":\"MySQL ordering results by specific field values\",\"datePublished\":\"2012-09-17T00:27:28+00:00\",\"dateModified\":\"2013-10-03T18:13:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\"},\"wordCount\":461,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\"},\"keywords\":[\"MySQL order by\",\"MySQL results order\",\"Order By Field\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\",\"url\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\",\"name\":\"MySQL ordering results by specific field values - Virendra's TechTalk\",\"isPartOf\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#website\"},\"datePublished\":\"2012-09-17T00:27:28+00:00\",\"dateModified\":\"2013-10-03T18:13:21+00:00\",\"description\":\"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort\",\"breadcrumb\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"TechTalk\",\"item\":\"https:\/\/www.virendrachandak.com\/techtalk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL\",\"item\":\"https:\/\/www.virendrachandak.com\/techtalk\/category\/mysql\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL ordering results by specific field values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#website\",\"url\":\"https:\/\/www.virendrachandak.com\/techtalk\/\",\"name\":\"Virendra's TechTalk\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\",\"name\":\"Virendra Chandak\",\"logo\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.virendrachandak.com\",\"https:\/\/www.facebook.com\/virendrachandak\",\"https:\/\/www.linkedin.com\/in\/virendrachandak\/\",\"https:\/\/x.com\/virendrachandak\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL ordering results by specific field values - Virendra's TechTalk","description":"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/","og_locale":"en_US","og_type":"article","og_title":"MySQL ordering results by specific field values - Virendra's TechTalk","og_description":"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort","og_url":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/","og_site_name":"Virendra&#039;s TechTalk","article_publisher":"https:\/\/www.facebook.com\/virendrachandak","article_author":"https:\/\/www.facebook.com\/virendrachandak","article_published_time":"2012-09-17T00:27:28+00:00","article_modified_time":"2013-10-03T18:13:21+00:00","author":"Virendra Chandak","twitter_card":"summary_large_image","twitter_creator":"@virendrachandak","twitter_site":"@virendrachandak","twitter_misc":{"Written by":"Virendra Chandak","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#article","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/"},"author":{"name":"Virendra Chandak","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"headline":"MySQL ordering results by specific field values","datePublished":"2012-09-17T00:27:28+00:00","dateModified":"2013-10-03T18:13:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/"},"wordCount":461,"commentCount":9,"publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"keywords":["MySQL order by","MySQL results order","Order By Field"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/","url":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/","name":"MySQL ordering results by specific field values - Virendra's TechTalk","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website"},"datePublished":"2012-09-17T00:27:28+00:00","dateModified":"2013-10-03T18:13:21+00:00","description":"In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort","breadcrumb":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.virendrachandak.com\/techtalk\/mysql-ordering-results-by-specific-field-values\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"TechTalk","item":"https:\/\/www.virendrachandak.com\/techtalk\/"},{"@type":"ListItem","position":2,"name":"MySQL","item":"https:\/\/www.virendrachandak.com\/techtalk\/category\/mysql\/"},{"@type":"ListItem","position":3,"name":"MySQL ordering results by specific field values"}]},{"@type":"WebSite","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website","url":"https:\/\/www.virendrachandak.com\/techtalk\/","name":"Virendra's TechTalk","description":"","publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17","name":"Virendra Chandak","logo":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.virendrachandak.com","https:\/\/www.facebook.com\/virendrachandak","https:\/\/www.linkedin.com\/in\/virendrachandak\/","https:\/\/x.com\/virendrachandak"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2vTtQ-iL","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":0,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}