8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 5 教程 / 5.6 常见查询示例 / 5.6.8 计算每天的访问次数
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

5.6.8 计算每日访问量5.6.8 Calculating Visits Per Day

以下示例显示如何使用位组函数来计算用户每月访问网页的天数。The following example shows how you can use the bit group functions to calculate the number of days per month a user has visited a Web page.

CREATE TABLE t1 (year YEAR, month INT UNSIGNED,
             day INT UNSIGNED);
INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2),
            (2000,2,23),(2000,2,23);

示例表包含表示用户访问该页面的年月日值。要确定每个月发生这些访问的不同天数,请使用以下查询:The example table contains year-month-day values representing visits by users to the page. To determine how many different days in each month these visits occur, use this query:

SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1
       GROUP BY year,month;

其返回:Which returns:

+------+-------+------+
| year | month | days |
+------+-------+------+
| 2000 |     1 |    3 |
| 2000 |     2 |    2 |
+------+-------+------+

查询计算每个年/月组合的表中出现多少个不同的天,并自动删除重复条目。The query calculates how many different days appear in the table for each year/month combination, with automatic removal of duplicate entries.