2014-11-01 44 views
1
SELECT userid, name 
FROM users 
WHERE nationality="US" 
ORDER BY name 

假設我想與userid=101nationality="US「用戶行顯示第一不管是什麼,並使用一個查詢做它真的很重要。MySQL的選擇,顯示與特定值第一

回答

4

嘗試FIELD

SELECT userid, name FROM users WHERE nationality="US" ORDER BY FIELD(user_id,101) DESC, name asc 

Field Documentation

0

試試這個:

SELECT * FROM (
    SELECT userid, name, IF(user = 101, 1 , 2) AS rank 
    FROM users WHERE nationality = "US" 
) AS subselect ORDER BY subselect.rank ASC, subselect.name ASC 
0

或者,你可以這樣做:

SELECT userid, name, (userid=101) AS test FROM users WHERE nationality="US" ORDER BY test DESC, name ASC