升序降序

ORDER BY x ASC  -- same as default
ORDER BY x DESC  -- highest to lowest
ORDER BY lastname, firstname  -- typical name sorting; using two columns
ORDER BY submit_date DESC  -- latest first
ORDER BY submit_date DESC, id ASC  -- latest first, but fully specifying order.
  • ASC = ASCENDINGDESC = DESCENDING
  • NULLs 甚至第一次來到 DESC
  • 在上面的例子中,INDEX(x)INDEX(lastname, firstname)INDEX(submit_date) 可以顯著提高效能。

但是……如上例所示,混合 ASCDESC 不能使用複合索引來獲益。INDEX(submit_date DESC, id ASC) 也不會有所幫助 - “DESC”在 INDEX 宣告中被語法識別,但被忽略了。