Want more features on Pastebin? Sign Up, it's FREE!
Guest

mysql order by field

By: zulius on Jun 28th, 2011  |  syntax: MySQL  |  size: 0.34 KB  |  views: 166  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. CREATE TABLE `foo` (
  2.   `bar` INT(3),
  3.   KEY `bar` (`bar`)
  4. ) ENGINE=MyISAM;
  5.  
  6. INSERT INTO `foo` VALUES (12), (57), (33), (7), (101);
  7.  
  8. SELECT `bar`
  9. FROM foo
  10. WHERE bar IN (101, 7, 12, 57)
  11. ORDER BY FIELD(bar, 101, 7, 12, 57);
  12.  
  13. /*
  14. +------+
  15. | bar  |
  16. +------+
  17. |  101 |
  18. |    7 |
  19. |   12 |
  20. |   57 |
  21. +------+
  22. 4 rows in set (0.00 sec)
  23. */
clone this paste RAW Paste Data