English 中文(简体)
MySQL-计算日期范围内的行出现次数,但将null转换为0,以便显示
原标题:MySQL - count row occurrences in a date range, but convert null to 0 so it shows

这是我当前的查询

SELECT DAYNAME(date_created) AS Day, 
COUNT(*) AS my_count FROM sometable 
WHERE (@date_created >=  2010-10-20 21:02:38  OR @date_created IS NULL) 
AND (@date_created <=  2010-10-27 21:02:38  OR @date_created IS NULL) 
GROUP BY DAY(date_created)

如果计数存在,它只返回当天的数据。

我一直在玩ifnull,但运气不好

我确信这是一个容易的问题,但我想不通!

如有任何帮助,不胜感激

问题回答

使用合作伙伴关系:

选择COALESCE(x,0)作为val FROM表;

如果x为null,它将返回0

MySQL缺乏在运行时生成记录集的方法(如PostgreSQL中的generate_steries)。

执行此查询的唯一方法是保留一个包含所有可能日期的表(或者,更好的是,一个较小的表,您可以与它本身交叉联接):

CREATE TABLE a (a INT PRIMARY KEY);

INSERT
INTO    a
VALUES
        (0),
        (1),
        …
        (99)

然后,您可以生成一个所有可能日期的列表,并将其留在您的表中:

SLEECT   2010-01-01  + INTERVAL 10000 * a1.a + 100 * a2.a + a3.a DAY AS dt,
        COUNT(date_created)
FROM    a a1
CROSS JOIN
        a a2
CROSS JOIN
        a a3
LEFT JOIN
        sometable
ON      date_created =  2010-01-01  + INTERVAL 10000 * a1.a + 100 * a2.a + a3.a DAY
WHERE   (a1.a, a2.a, a3.a) <= (0, 3, 65)
GROUP BY
        dt

这种情况:

(a1.a, a2.a, a3.a) <= (0, 3, 65)

意思是“记录365条记录”。

@符号描述了一个用户变量——你是从某个表中提取date_created吗?如果是,请删除变量中的@符号。

不确定如果date_created为null,为什么要包含它。也许一些示例行和所需的输出将有助于准确地说明您正在寻找的内容。祝你好运!:)

欢迎道格!我在本地运行了您的SQL的修改版本,即使是那些NULL日期,我也会得到结果。首先,要实时将null转换为其他值(“convert null to 0”),您需要使用MySQL语句IF,如果您对Oracle有所了解,它与DECODE命令非常相似。NULL值会自动计算为false,因此您只需编写:

SELECT IF(date_created,date_created,0) FROM sometable

当然0不再是日期,而NULL是日期。我发现DAYNAME函数只是传递NULL日期供您处理:

SELECT DAYNAME(date_created) day,COUNT(*) my_count 
FROM sometable
WHERE date_created IS NULL OR
 (date_created>= 2010-10-20 21:02:38  AND date_created <=  2010-10-27 21:02:38 )
GROUP BY DAY(date_created)

我从中得到的(通过一个示例数据集)是day的8个值:一周中的7天+NULL(带计数)。这有点道理。。。你无法将未知日期转换为一周中的某一天。

这可能是您SQL中错误的@符号,除非我误解了您的目的。

更新

根据您最后的评论,您需要接管PHP中的处理。提前构建days数组,然后将MySQL数据添加到现有数组中,以便所有的days都在那里(按顺序)。

PHP中的天数计数

//You may not even need "date_created IS NULL OR" in the where statement
$sql="SELECT DAYNAME(date_created) day,COUNT(*) my_count 
    FROM sometable
    WHERE date_created IS NULL OR
     (date_created>= 2010-10-20 21:02:38  
      AND date_created <=  2010-10-27 21:02:38 )
    GROUP BY DAY(date_created)";
//We re assuming you ve setup a MySQL connection in $conn
$result = @mysql_query($sql, $conn) or die(mysql_error());

//Starting data - Zero for every day
$days=array("Sunday"=>0,"Monday"=>0,"Tuesday"=>0,"Wednesday"=>0,
   "Thursday"=>0,"Friday"=>0,"Saturday"=>0);
while($row = mysql_fetch_array($result)) {
   $days[$row["day"]]+=$row["my_count"];
}
mysql_free_result($result);

//Preview the output
print_r($days);

使用时保持一致@





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签