Die gewünschte Abfrage ist diese
SELECT * FROM user
ORDER BY
IF(FIELD(user_id,1,4,5)=0,99999,FIELD(user_id,1,4,5)),user_name;
Ich habe Ihre Beispieldaten geladen und die Abfrage in MySQL 5.5.12 unter Windows ausgeführt
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.12 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
MySQL (Current test) :: USE example
Database changed
MySQL (Current example) :: DROP TABLE IF EXISTS user;
user_name VARCHAR(20)
);
INSERT INTO user VALUES
(1,'Peter'),(2,'John'),
(3,'Luke'),(4,'Tim'),
(5,'George'),(6,'Michael');
SELECT * FROM user;
SELECT * FROM user
ORDER BY
IF(FIELD(user_id,1,4,5)=0,99999,FIELD(user_id,1,4,5)),user_name;
Query OK, 0 rows affected (0.02 sec)
MySQL (Current example) :: CREATE TABLE user
-> (
-> user_id INT,
-> user_name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.08 sec)
MySQL (Current example) :: INSERT INTO user VALUES
-> (1,'Peter'),(2,'John'),
-> (3,'Luke'),(4,'Tim'),
-> (5,'George'),(6,'Michael');
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
MySQL (Current example) :: SELECT * FROM user;
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1 | Peter |
| 2 | John |
| 3 | Luke |
| 4 | Tim |
| 5 | George |
| 6 | Michael |
+---------+-----------+
6 rows in set (0.00 sec)
MySQL (Current example) :: SELECT * FROM user
-> ORDER BY
-> IF(FIELD(user_id,1,4,5)=0,99999,FIELD(user_id,1,4,5)),user_name;
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1 | Peter |
| 4 | Tim |
| 5 | George |
| 2 | John |
| 3 | Luke |
| 6 | Michael |
+---------+-----------+
6 rows in set (0.00 sec)
MySQL (Current example) ::
Probieren Sie es aus!!!
UPDATE 2011-07-26 13:37 EDT
Wenn die Reihenfolge der Liste wichtig ist, schreiben Sie die Liste einfach nach Bedarf.
BEISPIEL: Wenn Sie die Liste in der Reihenfolge 4,1,5 benötigen, sollte die Abfrage wie folgt aussehen:
SELECT * FROM user
ORDER BY
IF(FIELD(user_id,4,1,5)=0,99999,FIELD(user_id,4,1,5)),user_name;
Hier ist die Ausführung mit denselben Beispieldaten:
MySQL (Current example) :: SELECT * FROM user
-> ORDER BY
-> IF(FIELD(user_id,4,1,5)=0,99999,FIELD(user_id,4,1,5)),user_name;
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 4 | Tim |
| 1 | Peter |
| 5 | George |
| 2 | John |
| 3 | Luke |
| 6 | Michael |
+---------+-----------+
6 rows in set (0.00 sec)
MySQL (Current example) ::