How to query unique data and count?
CREATE TABLE IF NOT EXISTS `tab1` ( `id` int(6) unsigned NOT NULL, `rev` int(3) unsigned NOT NULL, `content` varchar(200) NOT NULL, PRIMARY KEY (`id`,`rev`) ) DEFAULT CHARSET=utf8; INSERT INTO `tab1` (`id`, `rev`, `content`) VALUES ('1', '1', 'The earth is flat'), ('2', '1', 'One hundred angels can dance on the head of a pin'), ('1', '2', 'The earth is flat and rests on a bull\'s horn'), ('1', '3', 'The earth is like a ball.'); CREATE TABLE IF NOT EXISTS `tab2` ( `id` int(6) unsigned NOT NULL, `rev` int(3) unsigned NOT NULL, `content` varchar(200) NOT NULL, PRIMARY KEY (`id`,`rev`) ) DEFAULT CHARSET=utf8; INSERT INTO `tab2` (`id`, `rev`, `content`) VALUES ('1', '1', 'The earth is flat'), ('2', '1', 'One hundred angels can dance on the head of a pin'), ('1', '2', 'The earth is flat and rests on a bull\'s horn'), ('1', '3', 'The earth is like a ball.');
SELECT tab1.id as tab1_id, tab2.id as tab2_id, COUNT(*) as qtde FROM `tab1` tab1 INNER JOIN (SELECT id FROM `tab2`) tab2 ON tab1.id = tab2.id GROUP BY tab1.id, tab2.id order by qtde; SELECT * FROM `tab1` tab1 INNER JOIN (SELECT id FROM `tab2`) tab2 ON tab1.id = tab2.id;