當select子句使用distinct關鍵字時,可能會與order by子句沖突,從而產生如下錯誤
錯誤:ORA-01791: not a SELECTed expression
根據sql語句的執行順序,distinct的執行順序優先於order by,因此distinct對select的字段去重后,order by只能在distinct后返回的結果集進行排序
因此order by 后的字段如果不在distinct后的結果集中,就會報錯
解決方案:1) order by子句的字段要只包含distinct后的字段
2) 先order by,然后再distinct