神奇的 SQL,Group By 真扎心,原來(lái)是這樣!
GROUP BY 后 SELECT 列的限制
CREATE?TABLE?tbl_student_class?(
??id?int(8)?unsigned?NOT?NULL?AUTO_INCREMENT?COMMENT?'自增主鍵',
??sno?varchar(12)?NOT?NULL?COMMENT?'學(xué)號(hào)',
??cno?varchar(5)?NOT?NULL?COMMENT?'班級(jí)號(hào)',
??cname?varchar(20)?NOT?NULL?COMMENT?'班級(jí)名',
??PRIMARY?KEY?(id)
)?COMMENT='學(xué)生班級(jí)表';
--?----------------------------
--?Records?of?tbl_student_class
--?----------------------------
INSERT?INTO?tbl_student_class?VALUES?('1',?'20190607001',?'0607',?'影視7班');
INSERT?INTO?tbl_student_class?VALUES?('2',?'20190607002',?'0607',?'影視7班');
INSERT?INTO?tbl_student_class?VALUES?('3',?'20190608003',?'0608',?'影視8班');
INSERT?INTO?tbl_student_class?VALUES?('4',?'20190608004',?'0608',?'影視8班');
INSERT?INTO?tbl_student_class?VALUES?('5',?'20190609005',?'0609',?'影視9班');
INSERT?INTO?tbl_student_class?VALUES?('6',?'20190609006',?'0609',?'影視9班');
FROM?tbl_student_class
GROUP?BY?cno,cname;
FROM?tbl_student_class
GROUP?BY?cno;
[Err] 1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.tbl_student_class.cname' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySQL 模式
語(yǔ)法支持類(lèi)
ONLY_FULL_GROUP_BY
ANSI_QUOTES
PIPES_AS_CONCAT
NO_TABLE_OPTIONS
NO_AUTO_CREATE_USER
數(shù)據(jù)檢查類(lèi)
NO_ZERO_DATE
NO_ENGINE_SUBSTITUTION
STRICT_TRANS_TABLES
默認(rèn)模式
SELECT?VERSION();
--?查看?sql_mode
SELECT?@@sql_mode;


SELECT?cno,cname,count(sno),MAX(sno)?
FROM?tbl_student_class
GROUP?BY?cno;
階

為什么聚合后不能再引用原表中的列

單元素集合也是集合
總結(jié)
參考
《SQL基礎(chǔ)教程》
《SQL進(jìn)階教程》
作者:青石路
原文:cnblogs.com/youzhibing/p/11516154.html
評(píng)論
圖片
表情
