SET @r=0;
select id,tname,CONCAT (taliase , addid) as aliase from
(
select * from (
SELECT id,tname,taliase,@r:=@r+1 as addid
FROM FF as f1
GROUP BY f1.tname,f1.taliase
UNION
SELECT id,tname,taliase,"" as addid FROM FF as f2
GROUP BY f2.taliase
ORDER BY taliase DESC
) as t1
order by t1.id,t1.addid
) as t2
group by t2.id
табличка была такая:
CREATE TABLE FF
(`id` int, `tname` varchar(20), `taliase` varchar(20))
;
INSERT INTO FF
(`id`, `tname`, `taliase`)
VALUES
(1, 'baranka', 'hleb'),
(2, 'bulka', 'hleb'),
(3, 'pirozhok', 'hleb'),
(4, 'konfeta','sladkoe'),
(5, 'karamel','sladkoe')
;