問題描述
我正在對兩個表進行連接.一個是用戶表,另一個是高級用戶列表.我需要讓高級會員首先出現在我的查詢中.但是,僅僅因為他們在高級用戶表中并不意味著他們仍然是高級會員 - 還有一個 IsActive 字段需要檢查.
I am doing a join on two tables. One is a user's table and the other a list of premium users. I need to have the premium members show up first in my query. However, just because they are in the premium user table doesn't mean they are still a premium member - there is an IsActive field that also needs to be checked.
所以基本上我需要按以下順序返回結果:
So basically I need to return the results in the following order:
- 活躍的高級用戶
- 普通和非活躍高級用戶
現在我有它如下:
SELECT Users.MemberId, PremiumUsers.IsActive FROM Users
LEFT JOIN PremiumUsers ON PremiumUsers.UserId = Users.Id
ORDER BY PremiumUsers.IsActive DESC
問題在于,它將非活躍高級會員置于非高級會員之上.
The problem with this is that it places non-active premium members above non-premium members.
(為此我使用的是 MS SQL Server 2005)
(I'm using MS SQL Server 2005 for this)
推薦答案
ORDER BY COALESCE(PremiumUsers.IsActive, 0) DESC
這會將 NULL 與非活動分組.
That will group the NULLs with not-actives.
這篇關于SQL 條件排序依據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!