SELECT userid, name
FROM users
WHERE nationality="US"
ORDER BY name
假設我想與userid=101
和nationality="US
「用戶行顯示第一不管是什麼,並使用一個查詢做它真的很重要。MySQL的選擇,顯示與特定值第一
SELECT userid, name
FROM users
WHERE nationality="US"
ORDER BY name
假設我想與userid=101
和nationality="US
「用戶行顯示第一不管是什麼,並使用一個查詢做它真的很重要。MySQL的選擇,顯示與特定值第一
嘗試FIELD
SELECT userid, name FROM users WHERE nationality="US" ORDER BY FIELD(user_id,101) DESC, name asc
試試這個:
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
或者,你可以這樣做:
SELECT userid, name, (userid=101) AS test FROM users WHERE nationality="US" ORDER BY test DESC, name ASC