简体   繁体   中英

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. DataBase columns:

ID UID STATUS CHECK_AT
  • ID - just a sequence number (1,2,3 and so on). Each time a script writing something into the DB, the number grows up.
  • UID - Key value. Let's say it's ID of a user. All DB contains about 3-5 differents UID
  • STATUS - with values 1 or 0. Let's say 1 is online, 0 is offline. Online status timeout is 10 minutes.
  • CHECK_AT - Time and date of script work, like 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). What I;m trying to do: To output summary online time of specific UIDs for a day; 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. Can you give me some advices, please?

you could add your conditions in WHERE clause like:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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