0

我有以下查询:

INSERT INTO StatisticalConsultationAgreement VALUES (
   queryType, entityCode, entityType, queryClass,queryTables,period, 
   COUNT(queryClass),  SUM(numberRecords),  SUM(recordsFound),
   SUM(NorecordsFound), NOW(), 'system');
SELECT
   MONTH(EndDateTimeProcessing),YEAR(EndDateTimeProcessing),
   entityType,
   entityCode,
   queryType,
   queryClass,
   EndDateTimeProcessing as period
FROM agreementFile
WHERE 
  MONTH(EndDateTimeProcessing)=MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH )) 
  AND YEAR(EndDateTimeProcessing)=YEAR(CURDATE())
GROUP BY entityType,entitycode,queryType, queryClass;

当我运行查询时,我得到下一个错误:

Error code 1111, SQL state HY000: Invalid use of group function
Line 1, column 1

Executed successfully in 0,002 s.
Line 5, column 2

为什么会出现这种情况?

如何解决?

4

2 回答 2

0

您正在将values语句与select中的语句混合insert。你只需要select. 这是我对你想要的最好的猜测:

INSERT INTO StatisticalConsultationAgreement 
    SELECT queryType, entityCode, entityType, queryClass,queryTables,period, 
           COUNT(queryClass),  SUM(numberRecords),  SUM(recordsFound),
           SUM(NorecordsFound), NOW(), 'system'
    FROM agreementFile
    WHERE MONTH(EndDateTimeProcessing)=MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH )) AND
          YEAR(EndDateTimeProcessing)=YEAR(CURDATE())
    GROUP BY entityType, entitycode, queryType, queryClass;

StatisticalConsultationAgreement但是,您还应该在insert语句中列出列名。

于 2013-08-24T17:58:15.670 回答
0

您没有分组EndDateTimeProcessing,当您尝试执行 Insert 时,它无法EndDateTimeProcessing从分组的行中确定应该采用哪个值。解决方案是将其添加到您的组子句中:

 GROUP BY entityType,entitycode,queryType, queryClass, EndDateTimeProcessing;

或者您使用函数组作为 MAX()、MIN() 等。

此致

编辑

正如 Gordon Linoff 所说,您还将查询与 INSERT 混合在一起,一切都应该由查询获得。

正确的语法应该是:

INSERT INTO StatisticalConsultationAgreement
SELECT
   'queryType', --I don't know what is the query type so i put it on single quote
   entityCode,
   entityType,
   queryClass,
   queryTables,
   MAX(EndDateTimeProcessing), --Period put on group function MAX, but it cant be grouped below or put into another group function
   COUNT(queryClass),  --
   SUM(numberRecords), --  ASUMING THOSE ARE COLUMNS IN  agreementFile
   SUM(recordsFound),  --
   SUM(NorecordsFound),-- 
   NOW(),
   'system'
FROM agreementFile
WHERE 
  MONTH(EndDateTimeProcessing)=MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH )) 
  AND YEAR(EndDateTimeProcessing)=YEAR(CURDATE())
GROUP BY entityType,entitycode,queryType, queryClass;

查询的字段MONTH(EndDateTimeProcessing),YEAR(EndDateTimeProcessing),被删除,因为我不知道 thouse 应该在哪里

于 2013-08-24T17:43:18.090 回答