mysql my.cnf sql_mode
时间: 2024-01-27 08:37:44 浏览: 189
my.cnf是MySQL的配置文件,用于配置MySQL的各种参数和选项。sql_mode是其中一个参数,它定义MySQL的SQL执行模式,包括如何处理SQL语句中的错误、如何处理日期和时间等。sql_mode的值可以通过my.cnf文件进行配置。例如,如果想要关闭严格模式,可以在my.cnf文件中将sql_mode设置为"NO_ENGINE_SUBSTITUTION"。这样,在执行SQL语句时,如果出现错误,MySQL将会尝试使用默认值或者空值代替而不是抛出错误。
相关问题
SELECT a.comm_id, c.Title house_keeper_name, ( SELECT build.id FROM erp_base.tb_base_masterdata_resource AS resource INNER JOIN erp_base.tb_base_masterdata_resource AS unit ON unit.id = resource.parent_id INNER JOIN erp_base.tb_base_masterdata_resource AS build ON build.id = unit.parent_id WHERE resource.id = a.resource_id ) resource_group FROM tb_charge_receipts_detail a LEFT JOIN erp_base.tb_base_masterdata_resource b ON b.id = a.resource_id LEFT JOIN erp_base.rf_dictionary c ON b.house_keeper = c.id GROUP BY a.comm_id,c.Title,resource_group 这个sql在mysql中报this is incompatible with sql_mode=only_full_group_by是为什么该怎么改
该错误是因为 MySQL 的版本更新,默认启用了 `ONLY_FULL_GROUP_BY` 模式,这种模式下,`GROUP BY` 必须包含所有非聚合字段。
在这个 SQL 语句中,`SELECT` 子句中的字段 `resource_group` 不在 `GROUP BY` 中,因此 MySQL 报错。
解决这个问题的方法有两个:
1. 关闭 `ONLY_FULL_GROUP_BY` 模式
可以通过修改 MySQL 的配置文件 `my.cnf` 或者在命令行中输入如下语句来关闭该模式:
```
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
```
这种方法会影响到整个 MySQL 实例的行为,需要谨慎使用。
2. 将 `SELECT` 子句中的所有字段都放入 `GROUP BY` 中
可以将 SQL 语句修改为:
```
SELECT a.comm_id, c.Title as house_keeper_name, (
SELECT build.id
FROM erp_base.tb_base_masterdata_resource AS resource
INNER JOIN erp_base.tb_base_masterdata_resource AS unit ON unit.id = resource.parent_id
INNER JOIN erp_base.tb_base_masterdata_resource AS build ON build.id = unit.parent_id
WHERE resource.id = a.resource_id
) as resource_group
FROM tb_charge_receipts_detail a
LEFT JOIN erp_base.tb_base_masterdata_resource b ON b.id = a.resource_id
LEFT JOIN erp_base.rf_dictionary c ON b.house_keeper = c.id
GROUP BY a.comm_id, c.Title, resource_group;
```
这种方法会使 SQL 语句更加冗长,但不会影响 MySQL 的全局设置。
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怎么处理
这是因为 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
```
阅读全文