php - SQL 计算营业时间为早上 10 点到早上 6 点的客户的不同访问次数

标签 php mysql database

我正在为每天早上 10 点到早上 6 点营业的俱乐部编写客户忠诚度软件。数据存储在 MYSQL 中,我想统计客户当月的总访问量。

我使用的是 count(distinct(date)) 但如果玩家在下午 5 点到达并一直待到凌晨 3 点,并在晚上 10 点和凌晨 2 点进行了 2 笔交易。它将被计为 2 次访问,而不是 1 次。

我有一个事务表,其中列出了以下列:

ps: anything in the brackets () is not real data. I get about 2000 transactions a day. I am also able to change the table structure

 Transaction_ID | Date(not Date/Time) | Customer_ID | Item | price | timestamp
   1            | 11-06-2015    (6pm) | Jane        | drink| 2.00  | 156165166
   2            | 09-06-2015    (2pm) | Jane        | drink| 2.00  | 1433858493
   3            | 10-06-2015    (3am) | Jane        | drink| 2.00  | 1433906073
   4            | 06-06-2015    (6pm) | Jane        | drink| 2.00  | 156165166

当前代码返回 {4, Jane}。我正在寻找的答案是 {3,Jane}。事务 {2,3} 应被视为一次访问

SELECT count(distinct(Date)) as visit, Customer_ID 
FROM transaction  
GROUP BY Customer_ID 
WHERE timestamp BETWEEN $timestamp1 AND $timestamp2
$timestamp1 = strtotime("first day of february +10am");
$timestamp2 = strtotime("first day of march +6am");

您建议如何准确统计下面的总访问量?我能够将表结构从日期更改为日期/时间。

The easiest answer with least changes to my codes.

SELECT count(DISTINCT(DATE(DATE_SUB(from_unixtime(timestamp),INTERVAL 6 HOUR))) as visit, Customer_ID 
FROM transaction  
GROUP BY Customer_ID 
WHERE timestamp BETWEEN $timestamp1 AND $timestamp2

最佳答案

最简单的方法是在 SQL 语句中将日期时间 (date,timestamp?) 字段向后移动 6 小时,然后您将得到一天的时间间隔从凌晨 4 点到中午 12 点:

DISTINCT(DATE(DATE_SUB(dt,INTERVAL 6 HOUR)))

SQLFiddle demo

关于php - SQL 计算营业时间为早上 10 点到早上 6 点的客户的不同访问次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31311823/

相关文章:

php - 必须如何配置服务器才能通过 php 脚本连接到邮箱

php - 在 Laravel 8 中使用用户名和密码进行身份验证时出现问题

mysql - 日期转换为字符串

java - 与本地 mysql 数据库连接时,getConnection 不是 JDBC 中的连接

iphone - Objective-C 的首选数据库管理系统(iPhone 应用程序)

php 无法删除多于一行

php - 如何在文本框上显示光标第一行

php - 使用PDO在mysql中插入数据的问题

php - 我应该在 PHP/MySQL 中存储序列化对象还是 JOIN 表?

database - 将数据导出为 C#/VB 格式,以便在 EF Code First 的数据库初始化中使用它们