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

倖福魔咒の 提交于 2019-11-29 06:06:28
Suriya Kumar

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'
Pradeep Kumar Prabaharan

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;

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.

Heinan Cabouly
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.

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