从不同地点访问计数

时间:2013-10-01 03:19:24

标签: mysql subquery

visit mysql表:

+---------+-------------+------------+--------+---------------------+
| visitId | visitSiteId | homeSiteId | userId |       time          |
+---------+-------------+------------+--------+---------------------+
|   1     |      1      |     1      |  1001  | 2013-09-01 11:50:12 |
|   2     |      1      |     1      |  1001  | 2013-09-03 12:50:12 |
|   3     |      2      |     1      |  1001  | 2013-09-04 13:50:12 |
|   4     |      2      |     1      |  1001  | 2013-09-07 11:00:00 |
|   5     |      2      |     1      |  1001  | 2013-09-09 12:32:12 |
|   6     |      2      |     1      |  1001  | 2013-09-13 15:48:12 |
|   7     |      2      |     2      |  1002  | 2013-09-01 11:00:12 |
|   8     |      2      |     2      |  1002  | 2013-09-02 12:50:12 |
|   9     |      2      |     2      |  1002  | 2013-09-05 13:50:12 |
|   10    |      1      |     2      |  1002  | 2013-09-06 11:50:12 |
|   11    |      1      |     2      |  1002  | 2013-09-07 12:50:12 |
|   12    |      2      |     2      |  1002  | 2013-09-15 13:50:12 |
+---------+-------------+------------+--------+---------------------+

记录访问日期时间,访问的站点和用户主页

我想找到如何从上面的数据输出3个报告。

  1. 返回任何拥有非主页站点访问次数> = HomeSite访问次数的用户;任何日期
  2. 退回所有拥有非主页网站访问次数的用户> = a最小输入的整数,例如非家庭计数> = 10
  3. 1和2都有日期范围,即时间> ='2013-09-01 11:50:12'和时间< ='2013-09-13 15:48:12'
  4. 我是MySQL的新手,并且很难使用子查询语法,所以非常感谢任何帮助: 下面的查询将返回主页访问和非主页访问的计数,但我无法理解子查询的综合结果

    SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site <> v.HomeSite GROUP BY v.user_id;
    SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site = v.HomeSite GROUP BY v.user_id;
    SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site <> v.HomeSite AND v.Time >= '2013-09-01 11:50:12' AND v.Time <= '2013-09-13 15:48:12' GROUP BY v.user_id;
    SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site = v.HomeSite AND v.Time >= '2013-09-01 11:50:12' AND v.Time <= '2013-09-13 15:48:12' GROUP BY v.user_id;
    

    这是MySQL Create Table和INSERT Data来帮助测试:

     CREATE TABLE `dbName`.`visit`(`visitId` INT(11) NOT NULL AUTO_INCREMENT, `visitSiteId` INT(11), `homeSiteId` INT(11), `userId` INT(11), `time` DATETIME, PRIMARY KEY (`visitId`));
    
    INSERT INTO `dbName`.`visit`(`visitSiteId`,`homeSiteId`,`userId`,`time`) VALUES
      (1,1,1001,'2013-09-01 11:50:12'),
      (1,1,1001,'2013-09-03 12:50:12'),
      (2,1,1001,'2013-09-04 13:50:12'),
      (2,1,1001,'2013-09-07 11:00:00'),
      (2,1,1001,'2013-09-09 12:32:12'),
      (2,1,1001,'2013-09-13 15:48:12'),
      (2,1,1002,'2013-09-01 11:00:12'),
      (2,1,1002,'2013-09-02 12:50:12'),
      (2,1,1002,'2013-09-05 13:50:12'),
      (1,1,1002,'2013-09-06 11:50:12'),
      (1,1,1002,'2013-09-07 12:50:12'),
      (2,1,1002,'2013-09-15 13:50:12');
    

3 个答案:

答案 0 :(得分:0)

没有尝试和测试..

SELECT Count(visitId) AS Visits, userId 
    FROM homeSiteId 
    GROUP BY visitId HAVING Count(visitId) > 2999

答案 1 :(得分:0)

这很简单。您必须使用CASE语句分别计算主站点和非主站点访问。

查询1

select userid
  from (
  select userid,
         count(case when visitsiteid = homesiteid then 1 end) homesitecount,   --count only those records where home site = visited site
         count(case when visitsiteid != homesiteid then 1 end) nonhomesitecount  --count only those records where home site != visited site
    from visit
   group by userid) as groupedvisit             --use this as inline view and filter out the users. 
 where nonhomesitecount >= homesitecount;

查询2

select userid,
       count(case when visitsiteid != homesiteid then 1 end) nonhomesitecount
  from visit
 group by userid
having count(case when visitsiteid != homesiteid then 1 end) > 3; 

下一个查询类似于Query1和2.您必须添加额外的where子句来限制日期。

where time >= lower_limit and time <= upper_limit

sqlfiddle

演示

答案 2 :(得分:0)

更新:如果您理解正确的内容,可以这样做

  

1。返回任何拥有非主页站点访问次数&gt; = HomeSite访问次数的用户;任何日期

SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 GROUP BY userid
HAVING visitsnonhome >= visitshome;
  

2。返回任何拥有非主页站点访问次数&gt; = a最小输入的整数

的用户
SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 GROUP BY userid
HAVING visitsnonhome >= 3;
  

3。 1和2都带有日期范围...

只需在上述查询中添加WHERE子句

即可
SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 WHERE time >= '2013-09-01 11:50:12' 
   AND time <= '2013-09-13 15:48:12'
 GROUP BY userid
HAVING visitsnonhome >= visitshome;

SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 WHERE time >= '2013-09-01 11:50:12' 
   AND time <= '2013-09-13 15:48:12'
 GROUP BY userid
HAVING visitsnonhome >= 3;

这是 SQLFiddle 演示