2014-02-24 121 views
0

我有以下查詢可以在我的測試框上正常運行,但不適用於我的生產框。問題是什麼?mysql查詢在不同的服務器上給出不同的結果

SELECT 
    *, 
    round(priceperkm * IFNULL(KMPlanned, projects.KM), 
      4) AS NEWVALUE, 
    round(PRICE * IFNULL(KMPlanned, projects.KM), 
      2) AS TotalPriceForItem, 
    SUM(((abs(FSP - LSP) + 1) * SI)/1000) AS KM_Completed, 
    round(SUM(((abs(FSP - LSP) + 1) * SI)/1000) * PRICE, 
      2) AS TotalPrice, 
    round((SUM(((abs(FSP - LSP) + 1) * SI)/1000) * PRICE)/(PRICE * IFNULL(KMPlanned, projects.KM)) * 100, 
      2) AS TotalPercent 
FROM 
    hdb.projects 
     join 
    biditems ON projects.id = biditems.project_id 
     join 
    lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id 
     left join 
    jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id 
     left join 
    detailsseismic ON jobsprocesscomplete.JOBNO = detailsseismic.JOBNO 
where 
    projects.PROJID = 1402013 
     and lookupprocess.ID = 13138  

錯誤:

2014/02/24 18:37:06 [error] [system.db.CDbCommand] CDbCommand::() failed: SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause. The SQL statement executed was: 


       . 
2014/02/24 18:37:06 [error] [exception.CDbException] exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause' in /data/intranet/html/yii-1.1.14/framework/db/CDbCommand.php:543 
Stack trace: 
#0 /data/intranet/html/yii-1.1.14/framework/db/CDbCommand.php(377): CDbCommand->queryInternal('', 0, Array) 
#1 /data/intranet/html/paradox/protected/controllers/GlobalController.php(260): CDbCommand->query() 
#2 /data/intranet/html/paradox/protected/controllers/ProjectsController.php(648): GlobalController->lookupprocesstotal('1402013', Array) 
#3 [internal function]: ProjectsController->actionUpdate('12768') 
#4 /data/intranet/html/yii-1.1.14/framework/web/actions/CAction.php(108): ReflectionMethod->invokeArgs(Object(ProjectsController), Array) 
#5 /data/intranet/html/yii-1.1.14/framework/web/actions/CInlineAction.php(47): CAction->runWithParamsInternal(Object(ProjectsController), Object(ReflectionMethod), Array) 
#6 /data/intranet/html/yii-1.1.14/framework/web/CController.php(308): CInlineAction->runWithParams(Array) 
#7 /data/intranet/html/yii-1.1.14/framework/web/CController.php(286): CController->runAction(Object(CInlineAction)) 
#8 /data/intranet/html/yii-1.1.14/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array) 
#9 /data/intranet/html/yii-1.1.14/framework/web/CWebApplication.php(282): CController->run('update') 
#10 /data/intranet/html/yii-1.1.14/framework/web/CWebApplication.php(141): CWebApplication->runController('projects/update') 
#11 /data/intranet/html/yii-1.1.14/framework/base/CApplication.php(180): CWebApplication->processRequest() 
#12 /data/intranet/html/paradox/index.php(31): CApplication->run() 
#13 {main} 
REQUEST_URI=/sidb/index.php?r=projects/update&id=12768 
HTTP_REFERER=http://intranet/sidb/index.php?r=projects/admin 

UPDATE 兩個服務器都在下面提供 SELECT @@ GLOBAL.sql_mode相同的結果; + ------------------- + | @@ GLOBAL.sql_mode | + ------------------- + | | + ------------------- + 1行中集(0.00秒)

mysql> SELECT @@SESSION.sql_mode; 
+--------------------+ 
| @@SESSION.sql_mode | 
+--------------------+ 
|     | 
+--------------------+ 
1 row in set (0.00 sec) 

mysql> show variables like 'sql_mode' 
    -> ; 
+---------------+-------+ 
| Variable_name | Value | 
+---------------+-------+ 
| sql_mode  |  | 
+---------------+-------+ 

,如果我在查詢的末尾添加group by lookupprocess.ID會發生什麼。它會返回相同的結果嗎?

+2

您的poduction服務器可能已打開ONLY_FULL_GROUP_BY選項,而測試服務器尚未打開,請參閱此鏈接瞭解詳細信息:http://dev.mysql.com/doc/refman/5.1/en/server-sql- mode.html#sqlmode_only_full_group_by – krokodilko

回答

0

我將兩臺服務器都更新到了存儲庫中最新的mysql版本。受影響的服務器有舊版本,我認爲它不支持sql語句。

1

您的select子句同時具有列和聚合函數(sum())。這會將查詢轉換爲只返回一行的聚合查詢,這可能是也可能不是您想要的。 (我懷疑你實際上是想group by東西。)

在任何情況下,這使用MySQL擴展。該擴展由系統變量ONLY_FULL_GROUP_BY控制。如果啓用此選項,則ANSI規則適用。閱讀它here

您看到的行爲表明ONLY_FULL_GROUP_BY在生產服務器上啓用,但在開發服務器上未啓用。您應該啓用它或在​​兩者中禁用它。

相關問題