Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatibility with Mysql 5.7(Expression #1 of ORDER BY clause is not in SELECT list)

Tags:

mysql

When I execute the following query I receive an Exception:

Error Code: 3065 Expression #1 of ORDER BY clause is not in SELECT list, references column 'webstore.level_depth' which is not in SELECT list; this is incompatible with DISTINCT

My Query:

SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
FROM `pj_category_shop` cs, `pj_category` c
INNER JOIN `pj_category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = 1 AND cl.id_shop = 2 )
WHERE (c.`active` = 1 OR c.`id_category` = 2)
  AND cs.`id_category` = c.`id_category`
  AND cs.`id_shop` = 2
  AND c.`id_category` != 1
  AND `level_depth` <= 2
  AND c.id_category IN (
    SELECT id_category
    FROM `pj_category_group`
    WHERE `id_group` IN (3)
  )
ORDER BY `level_depth` ASC, cl.`name` ASC;

Why is this happening?

like image 381
Suriya Kumar Avatar asked Jan 04 '17 13:01

Suriya Kumar


People also ask

What is the difference between MySQL 5.7 and MySQL 8?

MySQL 8.0 makes UTF8MB4 the default character set. SQL performance – such as sorting UTF8MB4 strings – has been improved by a factor of 20 in 8.0 as compared to 5.7. UTF8MB4 is the dominating character encoding for the web, and this move will make life easier for the vast majority of MySQL users.

What is MySQL compatibility mode?

MySQL and MariaDB have an SQL mode setting which changes how MySQL behaves. The SQL mode value is comprised of multiple flags like "STRICT_TRANS_TABLES, NO_ZERO_IN_DATE" . Each flag activates or disables a particular behavior.

Is MySQL 5.7 end of life?

MySQL 5.7 – Community end of life planned until October, 2023. Amazon RDS for MySQL will continue to support until the community EOL date. MySQL 8.0 – Community end of life planned until April, 2026.

Does MySQL 5.7 support InnoDB storage engine?

In MySQL 5.7, InnoDB is the default MySQL storage engine. Unless you have configured a different default storage engine, issuing a CREATE TABLE statement without an ENGINE clause creates an InnoDB table.


Video Answer


1 Answers

I have find the answer for my question.Actually mysql 5.7 contains 'ONLY_FULL_GROUP_BY' in sql mode.So we can't perform orderby in the element that is not in select list.we have to change it from

'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' 

into

'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

We can done this by executing the following queries

SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

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'
like image 79
Suriya Kumar Avatar answered Oct 21 '22 18:10

Suriya Kumar