I updated both servers to latest mysql version available in repository. The affected server had old version, which I believe did not support the sql statement.
mysql query gives different result on different servers
Question
I have the following query which runs fine on my test box but does not work on my production box. what is the problem?
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
Error:
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 Both servers give same result below SELECT @@GLOBAL.sql_mode; +-------------------+ | @@GLOBAL.sql_mode | +-------------------+ | | +-------------------+ 1 row in set (0.00 sec)
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 | |
+---------------+-------+
What will happen if i add group by lookupprocess.ID
at end of query. will it return same results?
Solution 2
OTHER TIPS
Your select
clause has both columns and aggregation functions (sum()
). This turns the query into an aggregation query that will return only one row, which may or may not be what you intend. (I suspect that you actually do want to group by
something.)
In any case, this uses a MySQL extension. The extension is controlled by a system variable ONLY_FULL_GROUP_BY
. When this is enabled, then the ANSI rules apply. Read about it here!.
The behavior that you are seeing suggests that ONLY_FULL_GROUP_BY
is enabled on the production server but not on the development server. You should either enable it or disable it in both.