Skip to content

Instantly share code, notes, and snippets.

@sofadesign
Created May 19, 2010 08:58
Show Gist options
  • Save sofadesign/406103 to your computer and use it in GitHub Desktop.
Save sofadesign/406103 to your computer and use it in GitHub Desktop.
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
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment