Tato definice tabulky dosáhne toho, co chcete.
CREATE TABLE `test` (
`Id` int(10) unsigned NOT NULL,
`Name` varchar(45) NOT NULL,
`RowNum` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`Id`,`Name`,`RowNum`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Naplnit tabulku daty
INSERT INTO test VALUES
(1,"test",null),
(1,"test",null),
(1,"test123",null),
(2,"test222",null),
(3,"test333",null);
Vyberte data z tabulky
SELECT * FROM test;
Výsledek
1, 'test', 1
1, 'test', 2
1, 'test123', 1
2, 'test222', 1
3, 'test333', 1
Udělat to v dotazu zde je poměrně hrubý způsob, jak to udělat.
select g.id,g.name,g.rownum
from (
select t.id,t.name,
@running:=if(@previous=concat(t.id,t.name),@running,0) + 1 as rownum,
@previous:=concat(t.id,t.name)
from test t
order by concat(t.id,t.name)
) g;