Advertisement
cahyadsn

Ranking

Dec 18th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.11 KB | None | 0 0
  1. CREATE TABLE person (id int, first_name varchar(20), age int, gender char(1));
  2.  
  3. INSERT INTO person VALUES (1, 'Bob', 25, 'M');
  4. INSERT INTO person VALUES (2, 'Jane', 20, 'F');
  5. INSERT INTO person VALUES (3, 'Jack', 30, 'M');
  6. INSERT INTO person VALUES (4, 'Bill', 32, 'M');
  7. INSERT INTO person VALUES (5, 'Nick', 22, 'M');
  8. INSERT INTO person VALUES (6, 'Kathy', 18, 'F');
  9. INSERT INTO person VALUES (7, 'Steve', 36, 'M');
  10. INSERT INTO person VALUES (8, 'Anne', 25, 'F');
  11.  
  12. SELECT    first_name,
  13.           age,
  14.           gender,
  15.           @curRank := @curRank + 1 AS rank
  16. FROM      person p, (SELECT @curRank := 0) r
  17. ORDER BY  age;
  18.  
  19. +------------+------+--------+------+
  20. | first_name | age  | gender | rank |
  21. +------------+------+--------+------+
  22. | Kathy      |   18 | F      |    1 |
  23. | Jane       |   20 | F      |    2 |
  24. | Nick       |   22 | M      |    3 |
  25. | Bob        |   25 | M      |    4 |
  26. | Anne       |   25 | F      |    5 |
  27. | Jack       |   30 | M      |    6 |
  28. | Bill       |   32 | M      |    7 |
  29. | Steve      |   36 | M      |    8 |
  30. +------------+------+--------+------+
  31. 8 rows in set (0.02 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement