我尝试过w3c网站的不同联接示例,但无法解决我的问题。
我能开始工作的是:
SELECT *
FROM chatters_online
WHERE TIME_TO_SEC(TIMEDIFF(NOW(),datumtijd )) < 300
但我想要的是每个聊天室的聊天数量:room1(12),room12(2)等。
CREATE TABLE IF NOT EXISTS `chatters_online` (
`room` int(10) unsigned NOT NULL,
`datumtijd` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`chatterID` int(10) unsigned NOT NULL DEFAULT '0',
`nick` varchar(20) NOT NULL,
UNIQUE KEY `unique_index` (`room`,`nick`),
KEY `room` (`room`),
KEY `nick` (`nick`),
KEY `datumtijd` (`datumtijd`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `chatrooms` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`roomname` varchar(20) NOT NULL,
`moderator` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `roomname` (`roomname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
我正在寻找的是每个聊天室计算所需的SQL。
答案 0 :(得分:1)
JOIN
和GROUP BY
应该会为您提供所需的结果
select CR.roomname, count(CO.chatterID) as ChattersCount
FROM chatrooms CR
JOIN chatters_online CO
on CO.room = CR.id
GROUP BY CR.roomname