Showing posts with label Mysql ORDER BY FIELD. Show all posts
Showing posts with label Mysql ORDER BY FIELD. Show all posts

Tuesday, 2 June 2015

Mysql: Custom ordering (order rows by my order)

CREATE TABLE groups(
  groupno INT(11) DEFAULT NULL,
  eventno INT(11) DEFAULT NULL
);
INSERT INTO groups VALUES 
  (1, 4),
  (2, 8),
  (4, 3),
  (1, 5),
  (3, 1);

Output custom ordered records:
SELECT groupno, eventno FROM groups ORDER BY FIELD(groupno, 3,2,1,4);
+---------+---------+
| groupno | eventno |
+---------+---------+
|       3 |       1 |
|       2 |       8 |
|       1 |       4 |
|       1 |       5 |
|       4 |       3 |