执行查询的问题(不在GROUP BY中)

时间:2011-09-14 14:42:28

标签: php mysql sql

我正在使用PHP并尝试在MySQL上执行查询,

当我使用let说的MySqlYog执行此查询时,我得到了结果,一切似乎都没问题。

查询:

SELECT 
  start_time AS `Date`,
  COUNT(1) AS `Count` 
FROM
  offline_execution_jobs 
WHERE start_time >= NOW() - INTERVAL 365 DAY
AND application_name LIKE 'SPLAT-ROLLING' OR application_name LIKE 'SPLAT'
GROUP BY (WEEK(start_time))
ORDER BY `Date` ASC ;

但问题是当我尝试从PHP执行查询时:

我收到了这个错误:

Invalid query: 'nolio_db.offline_execution_jobs.start_time' isn't in GROUP BY

如果有人遇到过这个问题,我会很高兴听到如何克服这个问题吗?

我需要以下列格式输出GROUP BY:

第一列的DATE格式的第一天和第二列的事件计数。

2011-01-09 03:28:54 | 38

2 个答案:

答案 0 :(得分:3)

如果您按照start_time的WEEK进行分组,那就是您应该选择的内容(而不是日期本身)。否则,您将如何报告计数?您希望显示每周的计数,而不是每个日期。

你的where子句中也有语义错误 - 你应该使用括号来明确设置AND和OR的优先顺序。

SELECT WEEK(start_time) AS Week, COUNT(1) AS Count 
FROM offline_execution_jobs 
WHERE start_time >= NOW() - INTERVAL 365 DAY 
AND (application_name LIKE 'SPLAT-ROLLING' 
OR application_name LIKE 'SPLAT' )
GROUP BY WEEK(start_time)
ORDER BY WEEK(start_time) ASC ;

此查询:

SELECT WEEK(start_time) AS Week, COUNT(1) AS Count 
FROM offline_execution_jobs 
WHERE start_time >= NOW() - INTERVAL 365 DAY 
AND (application_name LIKE 'SPLAT-ROLLING' 
OR application_name LIKE 'SPLAT' )
GROUP BY WEEK(start_time)
ORDER BY WEEK(start_time) ASC ;

返回以下结果:

1 21
2 50
3 15

但我需要这样的事情:

2011-01-04 08:05:24  21
2011-01-09 03:28:54  8
2011-01-16 06:08:18  11
2011-01-23 06:06:50  32

当从MySqlYog(MySql Windows客户端)执行此查询时,我得到了所需的结果,当我从php代码执行此查询时出现问题:

SELECT start_time AS WEEK, COUNT(1) AS COUNT 
FROM offline_execution_jobs 
WHERE start_time >= NOW() - INTERVAL 365 DAY 
AND (application_name LIKE 'SPLAT-ROLLING' 
OR application_name LIKE 'SPLAT' )
GROUP BY WEEK(start_time)
ORDER BY WEEK(start_time) ASC ;

这是我从php获得的错误:

Invalid query: 'nolio_db.offline_execution_jobs.start_time' isn't in GROUP BY

Whole query: SELECT start_time AS Date, COUNT(1) AS Count FROM offline_execution_jobs WHERE start_time >= NOW() - INTERVAL 250 DAY AND (application_name LIKE 'SPLAT-ROLLING' OR application_name LIKE 'SPLAT' ) GROUP BY WEEK(start_time) ORDER BY WEEK(start_time) ASC ;

这就是它在代码中的样子:

$query =    "SELECT start_time AS Date, COUNT(1) AS Count 
                    FROM offline_execution_jobs 
                    WHERE start_time >= NOW() - INTERVAL 250 DAY 
                    AND (application_name LIKE 'SPLAT-ROLLING' OR application_name LIKE 'SPLAT' )
                    GROUP BY WEEK(start_time)
                    ORDER BY WEEK(start_time) ASC ;";

    //echo "<br><br>$query<br><br>";

    // Create connection to DB                              
    $conn = mysql_connect($db_host, $db_user, $dp_pass);

    if (!$conn) 
    {   
        echo "<br/>Can't connect: $db_host";
        die('Could not connect: ' . mysql_error());
    }

答案 1 :(得分:1)

如果Date应该是表中找到的周的第一个start_time值,而不是根据日历的一周中的第一天,那么您可以简单地汇总start_time像这样:

SELECT 
  MIN(start_time) AS `Date`,
  COUNT(1) AS `Count` 
FROM
  offline_execution_jobs 
WHERE start_time >= NOW() - INTERVAL 365 DAY
AND application_name LIKE 'SPLAT-ROLLING' OR application_name LIKE 'SPLAT'
GROUP BY (WEEK(start_time))
ORDER BY `Date` ASC ;

顺便说一句,您可能仅按WEEK(start_time)进行分组。去年日期可能具有相同的周数,因此您的查询将分组不同的周数。要解决此问题,您只需将YEAR(start_time)添加到GROUP BY列表:

SELECT 
  MIN(start_time) AS `Date`,
  COUNT(1) AS `Count` 
FROM
  offline_execution_jobs 
WHERE start_time >= NOW() - INTERVAL 365 DAY
AND application_name LIKE 'SPLAT-ROLLING' OR application_name LIKE 'SPLAT'
GROUP BY YEAR(start_time), WEEK(start_time)
ORDER BY `Date` ASC ;