简体   繁体   English

在特定日期(mysql-php)中计算特定值的最简单方法是什么?

[英]What's the easiest way to count specific value in a specific date (mysql-php)?

(sorry for bad english and poor skill) Hello! (对不起,英语不好,技能不好),您好! I've got a mysql database which contains four columns and a cron job as a script, which requesting a status of a user every 10 minutes. 我有一个mysql数据库,其中包含四列和一个cron作业作为脚本,每10分钟请求一个用户状态。 DataBase columns: 数据库列:

ID UID STATUS CHECK_AT
  • ID - just a sequence number (1,2,3 and so on). ID-只是一个序列号(1、2、3等)。 Each time a script writing something into the DB, the number grows up. 每次脚本将一些内容写入数据库时​​,数字都会增加。
  • UID - Key value. UID-键值。 Let's say it's ID of a user. 假设它是用户的ID。 All DB contains about 3-5 differents UID 所有数据库包含大约3-5个不同的UID
  • STATUS - with values 1 or 0. Let's say 1 is online, 0 is offline. 状态-值为1或0。假设1处于在线状态,0处于离线状态。 Online status timeout is 10 minutes. 联机状态超时为10分钟。
  • CHECK_AT - Time and date of script work, like 2013-10-01 00:30:01 CHECK_AT-脚本工作的时间和日期,例如2013-10-01 00:30:01

Logic: every 10 minutes script is checking specific UIDs (written in other table) for online (1) or offline (0). 逻辑:每隔10分钟,脚本将检查特定的UID(写在其他表中)是否在线(1)或离线(0)。 What I;m trying to do: To output summary online time of specific UIDs for a day; 我想做的是:输出特定UID一天的摘要在线时间; week; 周; month etc 月等

I guess it should be elementary, like 我想应该是基本的

select count(id) from DB_NAME where date(check_at) = '2013-10-01';

for a one day 一天

select count(uid) from user_activity where date(check_at) between '2013-10-01' and '2013-10-07';

For a few days and so on. 等了几天。

But, my skill is to low to know, how I can count only online time (status=1) for a date. 但是,我的技能很低,我不知道该如何仅计算某个日期的在线时间(状态= 1)。 Can you give me some advices, please? 你能给我一些建议吗?

you could add your conditions in WHERE clause like: 您可以在WHERE子句中添加条件,例如:

select count(id) from your_table where date(check_at) = '2013-10-01' AND status = 1;

OR 要么

select count(uid) from user_activity where 
    date(check_at) between '2013-10-01' AND '2013-10-07'
    AND status = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM