So, my code looks like this
mysql_query("SELECT * FROM threads WHERE forum_id = '$id' ORDER BY type,posted DESC") or
die(mysql_error());
"posted" is the value of time() and is selected decreased to put the latest one at the top. "type" contains either 1 or 2. If it's 2, the thread is pinned. Currently, it sorts after posted but those which should be at the top (pinned) are at the bottom. Is there any fix that I'm missing?
Try: ORDER BY type DESC, posted DESC
By default, it sorts ascending. You need to specify the order for both fields you would like to order by.
Don't forget that the asc/desc in an order by applies to each individual field, not the entire order by clause.
ORDER BY type DESC, posted ASC
MySQL can also accept arbitrary logic for order by, so for more complicated sort requirements, you could have something like
ORDER BY IF(type=2, 0, 1) AS pinned ASC, posted DESC
as long the arbitrary logic returns something that's trivially sortable (eg a number or a string), there's no limit to how complicated the sorting can be.
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.