Make homepage|Add to favorites
 

MYSQL TOOLS
reviews, articles, tips & snippets

 
Buy SQL » MySQL » Custom ordering (order rows by my order)

Custom ordering (order rows by my order)

Author: rootsql Date: 1-11-2011, 22:51
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 |
+---------+---------+

Tags: Tips

Dear visitor, you went to the site as unregistered user.
We recommend you to Register or Sign In.

Comments:

Leave a comment
Information
Members of Guests cannot leave comments.