我安装了新的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);
如果您控制了服务器,并且正在运行无法轻松更改的旧代码,则可以调整服务器的 SQL 模式,并通过运行查询在启动期间删除"only_full_group_by"
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
或者将sql_mode=''
添加到您的 my.cnf 文件中。
显然,如果可能的话,最好更改代码,但如果没有,这将禁用该警告。
为了解决这个问题,请打开以下文件:
/etc/mysql/mysql.conf.d/mysqld.cnf
并在 [mysqld] 块下添加以下行
sql-mode=""
- 您可以按所述运行:
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
但是,如果您重新启动服务器,则可能需要重新运行它。
<小时 />- 您可以更新 mysql 服务器配置:
- 在 Windows 上,${MY_SQL_HOME}/my.cnf(或 my.ini) 在 Linux 上,以下之一:etc/my.cnf、/etc/mysql/my.cnf、/usr/etc/my.cnf
- 或 ~/.my.cnf
[mysqld]
# RESOLVE order-by-clause-is-not-in-select-list by removing ONLY_FULL_GROUP_BY from the sql-mode list
sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
如果你有phpMyAdmin:
1-转到变量选项卡
2 搜索标签"SQL 模式"
3-编辑内容并删除模式:"ONLY_FULL_GROUP_BY"
4-保存
注意:不要忘记验证逗号分隔符
with mamp pro
您无法直接编辑 my.cnf 文件。您必须使用 MAMP PRO 界面来编辑您的 my.cnf 文件。在菜单中,转到 MySQL> my.cnf>的文件>编辑模板。然后在[mysqld]
键下添加sql_mode=''
我有用,@pvgoran建议
SET @@sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
您可以根据需要运行此命令。
为了让->distinct('some_column')
为我工作,我需要在 mysql 模式下禁用ONLY_FULL_GROUP_BY
选项。
我没有编辑文件系统上的 mysql 配置文件,而是按照 Laravel 中的说明进行操作:语法错误或访问冲突:1055 错误并将其添加到config/database.php
:
'strict' => true,
'modes' => [
// 'ONLY_FULL_GROUP_BY', // disabled to allow grouping by one column
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
],
在我的项目中,modes
属性不存在,所以我只是简单地把那个坏男孩贴在那里。
注意:确保您了解完全擦除模式是不可取的,并且基本上不是严格模式,因此您将丢失有关超出 varchar 长度之类的调试警告/错误消息。诀窍是避免
sql_mode=""
.请注意,上面我如何使用 6 种模式并明确省略ONLY_FULL_GROUP_BY
.
试试这个:
SELECT 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
GROUP BY p.products_id,p.products_price
LIMIT :products_limit