简体   繁体   中英

Compare with last year/mont/week/day

I have a current setup where I sum all my projets from a database with my query who looks like this atm:

$query01 = "
  SELECT COUNT(project_id) as total_week
  FROM projects
  WHERE WEEK(CURDATE(), 1) between WEEK(project_start, 1) and WEEK(project_delivery, 1)
";

And an equal query for each year/month/day.

I also would like to output a comparison with an arrow up/down with last year/month/week/day for each.

How could I achieve something like this? Thanks in advance!

$thisweek = query("SELECT COUNT(project_id) as total_week
    FROM projects
    WHERE WEEK(CURDATE(), 1) between WEEK(project_start, 1) and WEEK(project_delivery, 1)");

$lastweek = query("SELECT COUNT(project_id) as total_last_week
    FROM projects
    WHERE WEEK(DATE_SUB(CURDATE(), INTERVAL 1 WEEK), 1) between WEEK(project_start, 1) and WEEK(project_delivery, 1)");

$comparison = $thisweek - $lastweek;

Something like that should do it, the 'query' function I used is assumed to pluck out just the single count in this scenario.

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