php + mysql, order by name + starting at specific id
MySQL:
id | name |
------------
1 | Joe |
2 | Craig |
3 | Shawn |
4 | Ryan |
5 | Seth |
PHP:
$a = mysql_query("SELECT * FROM table_name ORDER BY name DESC");
what I want to do though is, I want to start at id: 3, so it should output:
3,4开发者_开发知识库,5,1,2
You can use an expression in the ORDER BY:
SELECT id, name
FROM table_name
ORDER BY id < 3, id
Result:
3 Shawn 4 Ryan 5 Seth 1 Joe 2 Craig
I'd also advise you not to use SELECT *
and instead to list the columns explicitly.
EDIT : Mark is correct. The earlier query was syntactically incorrect. Using dummy aliasés should work!
Select id from
(
SELECT id FROM table_name
WHERE id >= 3
ORDER BY id ASC
) X
UNION
Select * from
(
SELECT id FROM table_name
WHERE id < 3
ORDER BY id ASC
) Y
This should give you 3,4,5,1,2
精彩评论