1. 問題說明
mysql incorrect datetime value '0000-0-0 00:00:00' for column
或者
this is incompatible with sql_mode=only_full_group_by
2. 原因
第一種情況是因為mysql
使用no_zero_date sql
模式,所以不允許將0000-00-00
保存為“偽日期”
第二種情況是因為mysql
使用ONLY_FULL_GROUP_BY
模式,所以如果沒有字段在 target list
以及 group by
字段中同時出現,那么 sql 就認為此條語句是不合法的,就會拋出錯誤。
3. 解決方法
1. 命令解決(mysql重啟后失效)
show variables like 'sql_mode';
variable_name | value |
---|---|
sql_mode | strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution |
找到其中需要去掉的sql_mode模式,如no_zero_date sql
set global sql_mode='strict_trans_tables,no_zero_in_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution';
2. 修改my.ini文件
需修改mysql配置文件,通過手動添加sql_mode的方式強制指定不需要的屬性,
在mysqld
下面添加代碼:
sql_mode=strict_trans_tables,no_zero_in_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution
注:
- 不同的系統,mysql 的配置文件名以及路徑不同
- Mac或Linux文件 /etc/my.cnf
- windows 在數據庫安裝目錄下的 my.ini