使用mySQL数据库中两个表的数据对行进行计数

pb3s4cty  于 11个月前  发布在  Mysql
关注(0)|答案(1)|浏览(106)

我试图收集()每天的新访问者,但我需要的数据在我的数据库中分为两个表。

mySQL表结构

我有两个表,第一个表看起来像这样-表注册新访客:

-- uniqueVisitors - as new visitors are seen they are added to this table

+--------------------+-------------+------+-----+---------+----------------+
| Field              | Type        | Null | Key | Default | Extra          |
+--------------------+-------------+------+-----+---------+----------------+
| uniqueVisitorID    | int         | NO   | PRI | NULL    | auto_increment |
| uuID               | varchar(36) | NO   |     | NULL    |                |
| websitePrivateHash | varchar(16) | NO   | MUL | NULL    |                |
| dateFirstSeen      | varchar(10) | NO   |     | NULL    |                |
| dateLastSeen       | varchar(10) | NO   |     | NULL    |                |
+--------------------+-------------+------+-----+---------+----------------+

字符串
我的第二个表看起来像这样-它记录了访问者每天的页面访问量:

-- visitorPageViews - visitors daily page views are tracked here. 

+--------------------+-------------+------+-----+---------+----------------+
| Field              | Type        | Null | Key | Default | Extra          |
+--------------------+-------------+------+-----+---------+----------------+
| visitorPageViewID  | int         | NO   | PRI | NULL    | auto_increment |
| websitePrivateHash | varchar(16) | NO   | MUL | NULL    |                |
| uuID               | varchar(36) | NO   |     | NULL    |                |
| date               | varchar(10) | NO   |     | NULL    |                |
| pageViews          | int         | NO   |     | 0       |                |
+--------------------+-------------+------+-----+---------+----------------+

我想做的

我试图从visitorPageViews获取访问者计数,其中dateFirstSeenuniqueVisitors不是同一天。
我试过这样做:

SELECT COUNT(visitorPageViews.uuID) AS count 
FROM uniqueVisitors, visitorPageViews 
WHERE uniqueVisitors.websitePrivateHash = visitorPageViews.websitePrivateHash 
AND visitorPageViews.websitePrivateHash = 'r2d2****c3po****' 
AND visitorPageViews.date = '2023-12-28' 
AND uniqueVisitors.dateFirstSeen != '2023-12-28';


但它返回了数百万的数字,可悲的是,这并不是新访客的真实反映。
如果有更好的方法来跟踪这些数据,我可以对数据结构进行更改,但我宁愿不要。

6jjcrrmo

6jjcrrmo1#

显然我们不能复制相同的条件,但考虑到目标,我将按如下方式处理查询。注意,我使用了一个连接条件(而不是where子句)来确保页面访问量在dateFirstSeen之后:

SELECT
      COUNT(p.uuID) AS count_all
    , COUNT(DISTINCT p.uuID) AS count_distinct
FROM visitorPageViews p
INNER JOIN uniqueVisitors v ON p.websitePrivateHash = v.websitePrivateHash
    AND v.dateFirstSeen < p.DATE
WHERE p.websitePrivateHash = 'r2d2****c3po****'

字符串
注:不确定是否需要count(distinct o.uuID)
| count_all|计数区分|
| --|--|
| 3 | 2 |
fiddle

相关问题