{"id":213,"date":"2012-02-07T14:04:38","date_gmt":"2012-02-07T14:04:38","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=213"},"modified":"2022-02-17T07:14:05","modified_gmt":"2022-02-17T07:14:05","slug":"sort-order","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/queries\/select\/sort-order","title":{"rendered":"ORDER BY"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Sorting Results<\/h4>\n\n\n\n<p>Use this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name ORDER BY row_name ASC\nor\nDESC<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">To Sort On Multiple Values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>select * FROM table_name ORDER BY HighestPriority DESC, NextRowName DESC, NextRowName DESC<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">ORDER BY With $wpdb->prepare<\/h4>\n\n\n\n<p>ORDER BY and %s does not work with $wpdb->prepare, in our tests the following order by gets ignored:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $sql = $wpdb->prepare(\"SELECT * FROM {$wpdb->prefix}table_devices \n                          ORDER BY %s ASC\n                        \", $SortOrder);<\/code><\/pre>\n\n\n\n<p>Whereas this works as expected.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $sql = \"SELECT * FROM {$wpdb->prefix}table_devices \n                          ORDER BY $SortOrder ASC\n                        \";<\/code><\/pre>\n\n\n\n<p>So it seems that  $wpdb->prepare() adding single quotes around the inserted name field causes it to break.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Date Order<\/h4>\n\n\n\n<p>ASC = oldest first<\/p>\n\n\n\n<p>DESC = most recent first<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">With GROUP BY<\/h4>\n\n\n\n<p>When using GROUP BY, if you also use ORDER BY it must come after the GROUP BY<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorting Results Use this To Sort On Multiple Values ORDER BY With $wpdb->prepare ORDER BY and %s does not work with $wpdb->prepare, in our tests the following order by gets ignored: Whereas this works as expected. So it seems that $wpdb->prepare() adding single quotes around the inserted name field causes it to break. Date Order [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-select"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":2712,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/213\/revisions\/2712"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}