Guest User

Untitled

a guest
May 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. DROP TABLE IF EXISTS `scores`;
  2.  
  3. CREATE TABLE `scores` (
  4. `id` int(11) NOT NULL AUTO_INCREMENT,
  5. `value` char(1) DEFAULT NULL,
  6. `level` int(11) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
  9.  
  10. LOCK TABLES `scores` WRITE;
  11. INSERT INTO `scores` (`id`,`value`,`level`)
  12. VALUES
  13. (1,'B',NULL),
  14. (2,'A',NULL),
  15. (3,'A',NULL),
  16. (4,'C',NULL),
  17. (5,'A',NULL);
  18. UNLOCK TABLES;
  19.  
  20. /* Update level value of all A scores */
  21. SET @rownum=0;
  22. UPDATE scores t, (SELECT @rownum:=@rownum+1 rownum, scores.* FROM scores WHERE value='A') r
  23. SET t.level = r.rownum WHERE (t.id = r.id);
  24.  
  25. /*
  26. NOW scores is:
  27.  
  28. id value level
  29. ----------------------
  30. 1 B NULL
  31. 2 A 1
  32. 3 A 2
  33. 4 C NULL
  34. 5 A 3
  35.  
  36. */
Add Comment
Please, Sign In to add comment