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