全部產品
Search
文件中心

OpenSearch:ORDER BY

更新時間:Jul 13, 2024

用於對一個或者多個欄位進行排序。預設為升序(ASC)。由於排序效能較差,要求排序時必須加上LIMIT子句。

文法格式

select:
  SELECT [ DISTINCT ]
    { projectItem [, projectItem ]* }
  FROM tableExpression
    ORDER BY { orderByItem [ASC|DESC] [,OrderByItem ASC|DESC]* }
    LIMIT N
    OFFSET M

樣本

  1. 簡單排序:

SELECT nid, brand, price, size FROM phone ORDER BY price LIMIT 1000
  1. 帶升降排序標誌的排序:

SELECT nid, brand, price, size FROM phone ORDER BY price ASC LIMIT 1000
  1. 多欄位排序:

SELECT nid, brand, price, size FROM phone ORDER BY size DESC, price DESC LIMIT 1000
  1. 返回價格排序後第11到第20名的結果:

SELECT nid, brand, price, size FROM phone ORDER BY price DESC LIMIT 10 OFFSET 10
  1. 不排序,隨機返回10個商品:

SELECT nid, brand, price, size FROM phone LIMIT 10