SQL 經典排序 Order By



SQL語法:

               Select * From 表名 Where 條件 Order By Case  When  條件 Then 1 Else 0 Desc


SQL實例:

                優先顯示AllPhoto表中有Price 和caption的圖片

               Select Top 20 r.* From   [AllPhoto] r With (Nolock) Where  r.PoiId = 2352
                          Order By
               Case When r.Caption Is Not Null And r.Caption != '' Then 1  Else 0 End Desc,
               Case When r.Price Is Not Null And r.Price > 0 Then 1  Else 0 End  Desc,
               r.SubmitTime  Desc,
               r.PhotoId Desc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章