1. 程式人生 > >ORDER BY 子句與 DISTINCT衝突

ORDER BY 子句與 DISTINCT衝突

在SQL中。如果使用SELECT DISTINCT(A) FROM TABLE ORDER BY 。。。時,後面ORDER BY 後的欄位名必須在前面選擇時出現,比如 select distinct(D1) from shop order by D1  這樣才不會出現衝突,如果是select distinct(D1) from shop order by D2 則會發生衝突;

還有就是如果ORDER BY 欄位的長度也要用上面的規則。如依據D1的長度來排序,如果寫成select distinct(D1) from shop order by len(D1) 那還是會出錯,必須寫成select distinct(D1),len(D1) from shop order by len(D1) 才可以。