按时间戳排序记录,但首先列出指定的ID

MySQL

+----+------------+
| id | timestamp  |
+----+------------+
| 1  | 1306922400 |
+----+------------+
| 2  | 1306926000 |
+----+------------+
| 3  | 1306929600 |
+----+------------+

PHP

$a = mysql_query("SELECT * FROM `table` ORDER BY ?? DESC, `timestamp` DESC");

Order by:

  1. Start where id is equal to 2

  2. Then order by timestamp

Order should be: 2, 3, 1

SELECT * FROM `table` ORDER BY `id`!=2 ASC,  `timestamp` DESC
// or
SELECT * FROM `table` ORDER BY `id`=2 DESC,  `timestamp` DESC
$a = mysql_query("SELECT * FROM `table` WHERE id = 2 ORDER BY `timestamp` DESC UNION SELECT * FROM `table` WHERE id != 2 ORDER BY `timestamp DESC");

you could use UNION... But I would avoid it.

$a = mysql_query("
                  (SELECT * FROM `table` WHERE id = 2 ) UNION 
                  (SELECT * FROM table ORDER BY timestamp DESC)
                ");

Considering i didn't an UNION ALL in the second query the record with id = 2 will get not duplicated.

SELECT * FROM `table` where id = 2
union 
SELECT * FROM `table` where id != 2 order by id, timestamp desc