简体   繁体   中英

MySQL Double Order

Is there a better way to write

SELECT users.id FROM `users`,`profiles` WHERE users.id = profiles.id && 
profiles.zipcode = '$zipcode' && users.id != '1' && users.id != '2'
&& profiles.picture != '' ORDER BY users.last_activity DESC LIMIT 0,11

(The code above is suppose to find all the users with a certain zipcode, and order them by the last_activity timestamp)

Also, is there anyway to sort it by last_activity and gender?

SELECT users.id 
FROM `users`
JOIN `profiles` ON users.id = profiles.id 
WHERE profiles.zipcode = '$zipcode' 
  AND users.id NOT IN (1,2)
  AND profiles.picture != '' 
ORDER BY users.last_activity DESC, users.gender 
LIMIT 0,11

Just add gender:

ORDER BY users.last_activity DESC, users.gender

Note that ordering by the second one (in this case gender ) will only happen if two users have the same last_activity value. Consider it a "tie-breaker" ordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM