SELECT DISTINCT p.*, u1.id AS directorId, CASE WHEN up1.member_type = 348 THEN u1.nickname ELSE up1.member_name END AS directorName FROM project p LEFT JOIN userproject up1 ON p.p_id=up1.p_id AND up1.director=1 LEFT JOIN sys_user u1 ON up1.member_id = u1.id LEFT JOIN userproject up ON p.p_id=up.p_id LEFT JOIN sys_user u ON up.member_id = u.id LEFT JOIN sys_user_department d ON d.user_id = u.id WHERE (p.enable = ? AND p.audit_status = ? AND p.organization_id = ? AND p.project_name IS NOT NULL) GROUP BY p.project_name,p.project_code,p.project_nature ORDER BY p.p_id DESC报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'spms.p.p_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by怎么处理
时间: 2024-01-30 10:03:49 浏览: 154
这是因为 MySQL 的 sql_mode 设置了 only_full_group_by,导致在 GROUP BY 子句中未包含的列不能在 SELECT 子句中使用。可以尝试以下两种处理方式:
1. 修改 sql_mode
将 MySQL 的 sql_mode 修改为不包含 only_full_group_by 这个选项,可以在 MySQL 配置文件 my.cnf 或者 my.ini 中找到 sql_mode 选项,将其修改为:
```
sql_mode=NO_ENGINE_SUBSTITUTION
```
然后重启 MySQL 服务即可生效。
2. 修改查询语句
在 SELECT 子句中将非聚合列 p.p_id 改为聚合列,例如使用 MAX(p.p_id)。修改后的查询语句如下:
```
SELECT DISTINCT MAX(p.p_id) AS p_id, p.project_name, p.project_code, p.project_nature, u1.id AS directorId, CASE WHEN up1.member_type = 348 THEN u1.nickname ELSE up1.member_name END AS directorName
FROM project p
LEFT JOIN userproject up1 ON p.p_id=up1.p_id AND up1.director=1
LEFT JOIN sys_user u1 ON up1.member_id = u1.id
LEFT JOIN userproject up ON p.p_id=up.p_id
LEFT JOIN sys_user u ON up.member_id = u.id
LEFT JOIN sys_user_department d ON d.user_id = u.id
WHERE (p.enable = ? AND p.audit_status = ? AND p.organization_id = ? AND p.project_name IS NOT NULL)
GROUP BY p.project_name,p.project_code,p.project_nature
ORDER BY p.p_id DESC
```
阅读全文