When I upgraded my ubuntu from 15.10 to 16.04 I have this error in my yii project:
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]:
Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
'v4master.A.id' which is not functionally dependent on columns in
GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by. The SQL statement executed was:
The sql code is follows:
$sql = "SELECT A.*,B.`type_name` FROM `".$this->tbl_auth_user_items."` A
INNER JOIN `".$this->tbl_auth_context_types."` B ON A.`type_id` = B.`type_id`
WHERE A.`user_id` = '".$user_id."' GROUP BY A.`type_id`, A.`type_value` ORDER BY A.`type_id` DESC";
return $this->_db->createCommand($sql)->queryAll();
Run this query at Mysql;
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Coild that your upgrade implies also a db upgrade to mysql 5.7 if so you must know that
starting for mysql 5.7 the use of column in select without aggreation function and not mentioned in group by is not allowed by default
if you need distinct result set ypu should use distinct clause and not group by
$sql = "SELECT DISTINCT
A.*,B.`type_name`
FROM `".$this->tbl_auth_user_items."` A
INNER JOIN `".$this->tbl_auth_context_types."` B ON A.`type_id` = B.`type_id`
WHERE A.`user_id` = '".$user_id."'
ORDER BY A.`type_id` DESC";
return $this->_db->createCommand($sql)->queryAll();
or ( but is deprecated) you can change the sql_mode for reverting to the previuos version behavior
fro
sql_mode = 'ONLY_FULL_GROUP_BY';
to
SET sql_mode = ''
Related
I have a following SQL which works in mysql version 5.6 but is breaking in mysql version 5.7.x.
SELECT * FROM (SELECT * FROM photos WHERE photoable_type = 'Mobilehome'
AND photoable_id IN (SELECT id FROM mobilehomes WHERE
mobilehomes.community_id = 1) AND photos.image_file_size IS NOT NULL
AND photos.is_published IS TRUE ORDER BY photos.priority ASC) AS tmp_table
GROUP BY photoable_id
It's throwing me following error:
Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'tmp_table.id' which is not functionally
dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
In this case or you change the sql mode for instrcut the db to work as mysql 5.6 version or you can adeguate your query to the new behavior
In this second case
If you use group by whithout aggregation function this mean that for all the column different from photoable_id you accept casual result
This mean that you could, probably, also accepted an aggregated result based greagtion function eg: on min() or max ()
assuming your tables containg col1, col2, .. the you must declare explicitally the column you need
SELECT photos.photoable_id, min(col1), min(col2),....
FROM photos
INNER JOIN mobilehomes ON mobilehomes.community_id = 1
AND photos.photoable_type = 'Mobilehome'
AND photos.photoable_id = mobilehomes.id
AND photos.image_file_size IS NOT NULL
AND photos.is_published IS TRUE
GROUP BY photos.photoable_id
ORDER BY photos.priority ASC
Looking to your code seems also that you could avoid the subquery
I am executing the below query
SELECT DISTINCT n.nid AS entity_id
FROM node n
INNER JOIN og_membership om ON n.nid=om.etid AND om.entity_type='node'
INNER JOIN foster_animal_capacity fc ON n.nid=fc.person_id
LEFT OUTER JOIN field_data_field_attributes fa ON n.nid=fa.entity_id
LEFT OUTER JOIN foster_person_black_outs fb ON n.nid=fb.person_id
WHERE -- n.nid = 1441663 AND
(om.gid = 464) AND (fc.animal_type_id = 3)
AND ((fc.capacity - fc.occupied)>=1)
AND ((fb.start_date IS NULL) OR (fb.end_date < 1523577600) OR (fb.start_date > 1522540800))
AND ((SELECT pid FROM `animal_history` WHERE `TYPE` = 'Foster Return'
AND pid = n.nid ORDER BY DATE_FORMAT(FROM_UNIXTIME( UNIX_TIMESTAMP( ) - MAX( CAST(`time` AS UNSIGNED) ) ) ,'%e')));
The query executing perfectly in MySql 5.5.5
But showing the below error in MySql 5.7.21
ERROR 3029 (HY000): Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
Why is this happening? And how can I overcome this issue?
I am not able to find any documentation for the error code 3029
As mentioned in MySQL documention :
MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default. For a description of pre-5.7.5 behavior, see the MySQL 5.6
For detail please check:
MySQL Handling of GROUP BY
I have this code, and it is working properly on the online server:
SELECT
DATE(`order`.`date`) AS `dater`,
COUNT(*) AS `orders-amount`,
SUM(`order`.`price`) AS `orders-income`,
(SELECT SUM(`amount`) FROM `paypal` WHERE `paypal`.`txn_id` != 'Bonus' AND YEAR(`dater`) = YEAR(`paypal`.`posted_date`) AND MONTH(`dater`) = MONTH(`paypal`.`posted_date`)) AS `total_charge`
FROM `order`
GROUP BY YEAR(`dater`), MONTH(`dater`)
ORDER BY `dater` DESC
But on localhost it gives an error as below:
#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'panel.order.date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Id I used this code for grouping then it working properly:
GROUP BY `dater`
This error appear only on localhost as I'm running on linux Mint and installing the apache, php, mysql & pypmyadmin
It means that different sql modes are used on local and remote servers.
Either you should change the mode or update your query like:
SELECT
YEAR(ANY_VALUE(`order`.`date`)) date_year,
MONTH(ANY_VALUE(`order`.`date`)) date_month,
COUNT(*) AS `orders-amount`,
SUM(`order`.`price`) AS `orders-income`,
(SELECT SUM(`amount`) FROM `paypal` WHERE `paypal`.`txn_id` != 'Bonus' AND YEAR(ANY_VALUE(`order`.`date`)) = YEAR(`paypal`.`posted_date`) AND MONTH(ANY_VALUE(`order`.`date`)) = MONTH(`paypal`.`posted_date`)) AS `total_charge`
FROM `order`
GROUP BY date_year, date_month
You have it in your error message:
this is incompatible with sql_mode=only_full_group_by
If you did not set it on purpose, I guess you got it by default and you have a different version on your local server (see here, it was made default at some point).
So all you need is to disable this mode, either by running SET sql_mode='';, or by following instructions here
I am attempting to execute the following query:
$query = "SELECT O. * AS NUM FROM (ab_order O)
LEFT JOIN ab_user U ON (O.id_user=U.id)
WHERE O.id IN ( SELECT OT.id_ab_order FROM ab_order_transaction OT
LEFT JOIN ab_transaction T ON (OT.id_ab_transaction = T.id)
LEFT JOIN ab_user U ON (T.id_user = U.id)
WHERE T.validated = 1 {$condTrans} ) {$condOrder}
ORDER BY {$orderCol} LIMIT $from, $numRecords ";
$queryDB = $DB->queryExec($query);
On the live server:
it works with MySql version 3.3.10.
PHP version 5.2.17.
But I need to use localhost:
XAMPP for linux, v. 1.7.7
PHP 5.3.8
MySql version 5.5.16.
On localhost it says:
MySQL error: 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS NUM
FROM (ab_order O)
LEFT JOIN ab_u' at line 1.
Is any easier way then uprgrade the live server Mysql database?
The aliasing of all the columns seems incorrect: SELECT O. * AS NUM. I'm unsure of why it would work on previous versions, but the as num should either be removed, or each column should be explicitly aliased.
You can define Alias for individual column in SELECT. Here is the issue
SELECT O. * AS NUM
Instead of this use
SELECT O. *
This is useful article Using Column Alias in SELECT Statement
I'm having an issue getting a COUNT() from a SQL query using Zend_Db_Table_Select, and I think it may be a possible bug because the SQL it should be generating actually works. Here's the Zend Select Query: ($this is a Zend_Db_Table, renamed to table1 in this example)
$select = $this->select();
$select->setIntegrityCheck(false);
// Select Count
$select->from($this, array("COUNT(*) as 'COUNT'"))
->joinLeft('users', 'table1.userID = users.userID')
->joinLeft('table2', 'users.anotherKey = table2.anotherKey');
// Add Where clause after join
$select->where('users.anotherKey = ?', $anotherKeyValue);
This gives the error:
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`
However, this query...
SELECT COUNT(*) AS 'count' FROM table1
LEFT JOIN users ON table1.userID = users.userID
LEFT JOIN table2 ON users.anotherKey = table2.anotherKey
WHERE users.anotherKey = [anotherKeyValue]
...returns the expected results with no errors when run against the database. Any ideas whats going on, why the error, and how to get around it?
have you tried to see actual query, that zend_db produce?