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

南楼画角 提交于 2019-12-05 14:37:45

问题


When I Executes the follwoing query i have received the 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;

I don't understand why this happening..??


回答1:


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'



回答2:


ORDER BY column should be column listed in the SELECT list

Add c.level_depth in your select list

Try:

SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite, c.level_depth
    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 c.`level_depth` ASC, cl.`name` ASC;



回答3:


Sql Feature Order by is as the name suggests used to order the Selected Columns on the basis of the Column mentioned in the below Syntax : Order by Column_Name ASC/DESC

So if you don't add the column using which you have decided to retrieve order set of data in the select clause you will get this error.




回答4:


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)
            ORDER BY c.`level_depth` ASC, cl.`name` ASC
            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));

To summarize, you need to have the ORDER BY in the context of the SELECT command, in which case, with the WHERE, FROM and INNER JOIN.



来源:https://stackoverflow.com/questions/41465332/incompatibility-with-mysql-5-7expression-1-of-order-by-clause-is-not-in-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!