Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS `scores`;
- CREATE TABLE `scores` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `value` char(1) DEFAULT NULL,
- `level` int(11) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
- LOCK TABLES `scores` WRITE;
- INSERT INTO `scores` (`id`,`value`,`level`)
- VALUES
- (1,'B',NULL),
- (2,'A',NULL),
- (3,'A',NULL),
- (4,'C',NULL),
- (5,'A',NULL);
- UNLOCK TABLES;
- /* Update level value of all A scores */
- SET @rownum=0;
- UPDATE scores t, (SELECT @rownum:=@rownum+1 rownum, scores.* FROM scores WHERE value='A') r
- SET t.level = r.rownum WHERE (t.id = r.id);
- /*
- NOW scores is:
- id value level
- ----------------------
- 1 B NULL
- 2 A 1
- 3 A 2
- 4 C NULL
- 5 A 3
- */
Add Comment
Please, Sign In to add comment