简体   繁体   English

MySQL单一查询可对多个日期范围内的行进行计数

[英]MySQL single query to count rows in multiple date ranges

I have to count the rows in several date ranges (today, week, month) from a mysql table. 我必须从mysql表中计算几个日期范围(今天,星期,月份)中的行。

Currently I am running through a loop in my PHP script and doing an individual query for each date range, which I know is a bad idea. 目前,我正在PHP脚本中循环运行,并对每个日期范围进行单独查询,我知道这是一个坏主意。

<?php
$now = "2016-04-21";
$today = $now;
$week = date( 'Y-m-d', strtotime( '-7 day', strtotime( $now ) ) );
$month = substr( $now, 0, 7 );

$res1 = mysql_query("SELECT SUM(pageview) FROM statistics WHERE user = '$id' AND DATE_FORMAT( date, '%Y-%m-%d' ) = '$today'" );
$res2 = mysql_query("SELECT SUM(pageview) FROM statistics WHERE user = '$id' AND ( t.date BETWEEN ( '$week' ) AND ( '$today' ) )");
$res3 = mysql_query("SELECT SUM(pageview) FROM statistics WHERE user = '$id' AND DATE_FORMAT( t.date, '%Y-%m' ) = '$month'" );

So I would like to use a SINGLE QUERY to do so. 因此,我想使用单查询。

I have found count rows in multiple date ranges and query for grabbing multiple date ranges but I'm not sure which is the best, and how to do in my case 我发现了多个日期范围内的计数行,查询了 多个日期范围的 查询,但是我不确定哪个是最好的,以及如何处理我的情况

If you really want to retrieve the three values using one query: 如果您真的想使用一个查询来检索三个值:

$res = mysql_query("
SELECT sum1, sum2, sum3 
FROM (SELECT SUM(pageview) AS sum1 FROM statistics WHERE user = '$id' AND DATE_FORMAT( date, '%Y-%m-%d' ) = '$today') AS T1
JOIN (SELECT SUM(pageview) AS sum2 FROM statistics WHERE user = '$id' AND ( t.date BETWEEN ( '$week' ) AND ( '$today' ) )) AS T2
JOIN (SELECT SUM(pageview) AS sum3 FROM statistics WHERE user = '$id' AND DATE_FORMAT( t.date, '%Y-%m' ) = '$month') AS T3");

Although this is not much better performance wise, i don't really see a reason to do this. 尽管这并不是更好的性能选择,但我真的没有理由这样做。

PS it's a bad habit to insert variables into queries like this. PS:将变量插入这样的查询中是一个坏习惯。 In this case your safe but if you use user inputted variables you're open to SQL injection. 在这种情况下,您可以放心使用,但如果使用用户输入的变量,则可以进行SQL注入。 Try learning prepared statements 尝试学习准备好的陈述

You can combine all your conditions by OR to select all data in one query. 您可以按OR组合所有条件,以在一个查询中选择所有数据。 And this construction can be used in SELECT to sum rows that meet some condition 并且此构造可用于SELECT中以求和满足某些条件的行

SUM(IF(condition, sum_column_name, 0))

Your query can look like this if you want to return one row with three sums 如果您想返回一行包含三笔总和的查询,则查询看起来像这样

SELECT
  SUM(IF(t.date = '$today', pageview, 0)) AS pageview_today,
  SUM(IF(t.date BETWEEN '$week' AND '$today', pageview, 0)) AS pageview_week,
  SUM(IF(DATE_FORMAT(t.date, '%Y-%m') = '$month', pageview, 0)) AS pageview_month
FROM statistics t
WHERE user = '$id' AND (
  t.date = '$today'
  OR (t.date BETWEEN '$week' AND '$today')
  OR DATE_FORMAT(t.date, '%Y-%m') = '$month'
)

Or you can make 3 queries and combine them with UNION as was mentioned by @quazardous. 或者,您可以进行3个查询,并将它们与UNION结合使用,如@quazardous所述。 And this method will give you 3 different rows. 并且此方法将为您提供3个不同的行。

Use Union ? 使用联盟?

Select a from B
Union
Select B from C
...

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

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