Struktura komentarzy:
--
-- Struktura tabeli dla `comments`
--
CREATE TABLE `comments` (
`comment_id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`comment_item_id` mediumint(8) UNSIGNED NOT NULL DEFAULT '0',
`comment_type` char(2) NOT NULL DEFAULT '',
`comment_name` varchar(50) NOT NULL DEFAULT '',
`comment_message` text NOT NULL,
`comment_datestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
`comment_ip` varchar(20) NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`comment_id`),
KEY `comment_datestamp` (`comment_datestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin2 AUTO_INCREMENT=973 ;
Gdzie comment_name określa kto napisał komentarz. Oraz struktura ocen:
--
-- Struktura tabeli dla `ratings`
--
CREATE TABLE `ratings` (
`rating_id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`rating_item_id` mediumint(8) UNSIGNED NOT NULL DEFAULT '0',
`rating_type` char(1) NOT NULL DEFAULT '',
`rating_user` mediumint(8) UNSIGNED NOT NULL DEFAULT '0',
`rating_vote` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`rating_datestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
`rating_ip` varchar(20) NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`rating_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin2 AUTO_INCREMENT=1594 ;
Tutaj rating_user określa kto coś ocenił.
No i moje zapytanie wygląda tak:
SELECT u.user_name username, u.user_id idusera, COUNT( k.comment_name ) komentarzy, COUNT( o.rating_user ) ocen
FROM users u
LEFT JOIN comments k ON k.comment_name = u.user_id
LEFT JOIN ratings o ON o.rating_user = u.user_id
GROUP BY username
ORDER BY komentarzy DESC , ocen DESC LIMIT 5