This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `names` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`group_id` int(10) unsigned NOT NULL, | |
`name` varchar(100) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=1; | |
CREATE TABLE `groups` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`name` varchar(50) DEFAULT NULL, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select id, name, group_id from | |
( | |
select | |
id, | |
name, | |
group_id, | |
@group_rank := IF(@current_group=group_id, @group_rank + 1, 1) as group_rank, | |
@current_group := group_id | |
from ( | |
select id, name, group_id, CONCAT(group_id, '-', round(rand() * 100)) as rand_rank |