php - 如何解决 "ORDER BY clause is not in SELECT list"导致 MySQL 5.7 的 SELECT DISTINCT 和 ORDER BY

标签 php mysql

我安装了新的 Ubuntu,但我的代码遇到了 MySQL 问题。

( ! ) Warning: PDOStatement::execute(): SQLSTATE[HY000]: General error: 3065 
Expression #2 of ORDER BY clause is not in SELECT list, references column 'clicshopping_test_ui.p.products_date_added' which is not in SELECT list; this is incompatible with DISTINCT 
in /home/www//boutique/includes/OM/DbStatement.php on line 97s

似乎 MySQL 5.7 不允许这样的请求:

select .... distinct with  order by rand(), p.products_date_added DESC

如果我使用它,它会起作用:

select distinct .... with  order by rand(), 

如何解决这种情况?

PHP 中的我的 SQL 请求

 $Qproduct = $OSCOM_PDO->prepare('select distinct p.products_id,
            p.products_price
            from :table_products p left join :table_specials s on p.products_id = s.products_id
            where products_status = :products_status
            and products_view = :products_view
            and p.products_archive = :products_archive
            order by rand(),
            p.products_date_added DESC
            limit :products_limit');
                  $Qproduct->bindInt(':products_status', 1);
                  $Qproduct->bindInt(':products_view', 1);
                  $Qproduct->bindInt(':products_archive', 0);
                  $Qproduct->bindInt(':products_limit', 
                  (int)MODULE_FRONT_PAGE_NEW_PRODUCTS_MAX_DISPLAY);

最佳答案

如果您可以控制服务器并且正在运行无法轻易更改的遗留代码,您可以 adjust the SQL mode of the server并在启动期间通过运行查询删除“only_full_group_by”

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

或通过将 sql_mode='' 添加到您的 my.cnf 文件。

显然,如果有可能,最好更改您的代码,但如果没有,这将禁用该警告。

关于php - 如何解决 "ORDER BY clause is not in SELECT list"导致 MySQL 5.7 的 SELECT DISTINCT 和 ORDER BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829911/

相关文章:

php - 通过 PHP foreach 循环按钮将数据提交到 mysql 数据库的单行

php - 500内部服务器错误在PHP中引用文件时?

php - 如何在 PHP 中运行存储在 .text 文件中的一系列 SQL 语句?

php - 使用 php、mysql 和 html 创建登录表单但无法登录

php - 在 PHP 中看不到 Python 模块

c# - 使用 MYSQL 数据库在 .NET 中设置和部署

mysql - MySQL 错误加入 WHERE 子句

php - 创建数据库字段与通过编码生成数据

php - 我如何根据另一个查询从 mysql 查询中排除一些记录

mysql - 查询过滤掉包含html标签的结果