有2个表如下;一个是站点信息,一个是站点不同时间的访问量,
现在要获取总访问量大于200的网站;
先执行如下sql,不包括having子句看一下,获得的是所有站点的总访问量;
这应是一个子查询,因为它的from后面是2个表的连接获得的结果集,
where无法与聚合函数一起使用,having可用于筛选进行了sum()后的结果;加上having子句之后结果如下,
insert into mysites (sitename, siteurl, alexa, country) values ('Google','https:\/\/www.google.cm\/',1, 'USA' );
insert into mysites (sitename, siteurl, alexa, country) values ('淘宝', 'https://www.taobao.com/', 13 , 'CN');
insert into mysites (sitename, siteurl, alexa, country) values ('百度' , 'http://www.baidu.com/', 4689, 'CN');
insert into mysites (sitename, siteurl, alexa, country) values ('微博' , 'http://weibo.com/', 20, 'CN');
insert into mysites (sitename, siteurl, alexa, country) values ('Facebook', 'https://www.facebook.com/', 3, 'USA');
insert into mysites (sitename, siteurl, alexa, country) values ('stackoverflow', 'http://stackoverflow.com/', 0, 'IND');
insert into accesslog (siteid, count, acdate) values (1,45,'2016-05-10');
insert into accesslog (siteid, count, acdate) values (3, 100, '2016-05-13');
insert into accesslog (siteid, count, acdate) values (1, 230, '2016-05-14');
insert into accesslog (siteid, count, acdate) values (2, 10, '2016-05-14');
insert into accesslog (siteid, count, acdate) values (5, 205, '2016-05-14');
insert into accesslog (siteid, count, acdate) values (4, 13, '2016-05-15');
insert into accesslog (siteid, count, acdate) values (3, 220, '2016-05-15');
insert into accesslog (siteid, count, acdate) values (5, 545, '2016-05-16');
insert into accesslog (siteid, count, acdate) values (3, 201, '2016-05-17');
select * from mysites;
select * from accesslog;
SELECT mysites.sitename, SUM(accesslog.count) AS 访问量 FROM (accesslog
INNER JOIN mysites
ON accesslog.siteid = mysites.siteid)
GROUP BY mysites.sitename
HAVING SUM(accesslog.count) > 200;