解決方案:Mysql for mac 出現 Expression #11 of SELECT list is not in GROUP BY clause
阿新 • • 發佈:2021-01-10
一、問題描述
- 自從博主更換
mysql-5.7.31-macos10.14-x86_64
之後出現Expression #11 of SELECT list is not in GROUP BY clause
這樣的異常 - 這主要是因為在mysql5.7之後,
sql_model
默認了ONLY_FULL_GROUP_BY
,在select語句中跟sqlserver一樣,如果使用group by語句,查詢欄位必須也得出現在group by之後或者使用聚合函式,否則將會報錯。
select @@global.sql_mode
二、解決方案
- 臨時解決方案
- 在資料庫管理工具中重新設定sql_model。
將原先查詢出來的set
sql_model
去掉ONLY_FULL_GROUP_BY
即可臨時解決。
- 在資料庫管理工具中重新設定sql_model。
- 更改配置檔案
- 首先停止MySQL服務,在etc下建立my.cnf檔案
輸入i進入插入模式,將以下文件複製進去sudo /usr/local/mysql/support-files/mysql.server stop sudo vim /etc/my.cnf
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL [client] default-character-set=utf8 [mysqld] log-error=/usr/local/mysql/data/mysqld.log # skip-grant-tables # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. # basedir = ..... # datadir = ..... # port = ..... # server_id = ..... # socket = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
複製之後按esc,輸入:wq儲存退出即可。啟動MySQL
sudo /usr/local/mysql/support-files/mysql.server restart
三、Sql_model
- 常用sql_mode
- ①ONLY_FULL_GROUP_BY
對於GROUP BY聚合操作,如果在SELECT中的列,沒有在GROUP BY中出現,那麼這個SQL是不合法的,因為列不在GROUP BY從句中
- ②NO_AUTO_VALUE_ON_ZERO
該值影響自增長列的插入。預設設定下,插入0或NULL代表生成下一個自增長值。如果使用者希望插入的值為0,而該列又是自增長的,那麼這個選項就有用了。
- ③STRICT_TRANS_TABLES
如果一個值不能插入到一個事務中,則中斷當前的操作,對非事務表不做限制
- ④NO_ZERO_IN_DATE
不允許日期和月份為零
- ⑤NO_ZERO_DATE
mysql資料庫不允許插入零日期,插入零日期會丟擲錯誤而不是警告
- ⑥ERROR_FOR_DIVISION_BY_ZERO
在insert或update過程中,如果資料被零除,則產生錯誤而非警告。如果未給出該模式,那麼資料被零除時Mysql返回NULL
- ⑦NO_AUTO_CREATE_USER
禁止GRANT建立密碼為空的使用者
- ⑧NO_ENGINE_SUBSTITUTION
如果需要的儲存引擎被禁用或未編譯,那麼丟擲錯誤。不設定此值時,用預設的儲存引擎替代,並丟擲一個異常
- ⑨PIPES_AS_CONCAT
將"||"視為字串的連線操作符而非或運算子,這和Oracle資料庫是一樣是,也和字串的拼接函式Concat想類似
- ⑩ANSI_QUOTES
不能用雙引號來引用字串,因為它被解釋為識別符