Skip to content

修复MySQL 8+ SQL执行报错问题 #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

liujiaqi222
Copy link

修复MySQL 8+下的GROUP BY兼容性问题

问题描述

  1. 字段别名未传递

在MySQL 8.0+环境下运行系统时,管理界面出现SQL语法错误。经排查发现两个因SQL别名使用不一致导致的问题:

image

$query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day, count(*) as count, sum(pay_price) as price");
$query->group("FROM_UNIXTIME(add_time, '$timeUnix')"); // ❌ 使用原始表达式

GROUP BY应使用SELECT中定义的别名day而非原始表达式。MySQL 8.0默认的ONLY_FULL_GROUP_BY模式导致的严格校验问题。

  1. ​ORDER BY与SELECT字段不一致

在crmeb/app/dao/order/StoreOrderDao.php存在select的列和实际orderby的列不一致的情况:

$query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
$query->group("FROM_UNIXTIME($group, '$timeUinx')");
})
->order('add_time ASC')->select()->toArray(); // ❌ 应使用别名time

附录:报错日志
20250306_error.log

最后,非常感谢CRMEB团队开源这个项目,以及感谢大佬录制的B站视频。

@liujiaqi222 liujiaqi222 deleted the branch crmeb:master March 7, 2025 14:54
@liujiaqi222 liujiaqi222 closed this Mar 7, 2025
@liujiaqi222 liujiaqi222 deleted the master branch March 7, 2025 14:54
@liujiaqi222 liujiaqi222 restored the master branch March 7, 2025 14:58
@liujiaqi222 liujiaqi222 reopened this Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant