您好,登錄后才能下訂單哦!
今天給大家介紹一下group by到底是什么。文章的內(nèi)容小編覺(jué)得不錯(cuò),現(xiàn)在給大家分享一下,覺(jué)得有需要的朋友可以了解一下,希望對(duì)大家有所幫助,下面跟著小編的思路一起來(lái)閱讀吧。
本來(lái)今天是要接著整理日志系統(tǒng)的優(yōu)化的,但是自己對(duì)梳理一些名詞理解有些欠缺,思來(lái)想去還是想討論下group by語(yǔ)法問(wèn)題,這個(gè)問(wèn)題我以前舉例說(shuō)明過(guò),腦海里就一致這么認(rèn)為:在SELECT 列表中所有未包含在組函數(shù)中的列都應(yīng)該是包含在 GROUP BY 子句中的。
驗(yàn)證:
-- ----------------------------
-- Table structure for mytbl2
-- ----------------------------
DROP TABLE IF EXISTS `mytbl2`;
CREATE TABLE `mytbl2` (
`id` int(11) NULL DEFAULT NULL,
`NAME` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`dept` int(11) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of mytbl2
-- ----------------------------
INSERT INTO `mytbl2` VALUES (1, 'zhangsan', 33, 101);
INSERT INTO `mytbl2` VALUES (2, 'li4', 34, 101);
INSERT INTO `mytbl2` VALUES (3, 'w5', 34, 102);
INSERT INTO `mytbl2` VALUES (4, 'zhao6', 34, 102);
INSERT INTO `mytbl2` VALUES (5, 't7', 36, 102);
目的:查詢表中年齡最大的員工部門,名字
select `NAME`,dept,MAX(age)from mytbl2 GROUP BY dept;
此條sql看上去語(yǔ)義正確,但邏輯卻錯(cuò)誤,結(jié)果與預(yù)想不一致
分析:先找出表中最大年齡,作為臨時(shí)表,再聯(lián)查
SELECT *FROM mytbl2 m INNER JOIN (select dept,MAX(age) as maxagefrom mytbl2 GROUP BY dept)ab on ab.dept=m.dept and m.age=ab.maxage;
當(dāng)然本表因?yàn)樘厥獗恚嬖趩螛?biāo)自關(guān)聯(lián)
1.語(yǔ)法為什么不會(huì)報(bào)錯(cuò)?
在高版本sql中,本語(yǔ)法是不會(huì)報(bào)錯(cuò)的
2.但是到底group by語(yǔ)法是怎樣的?
查詢官網(wǎng):
https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html
一開(kāi)始我考慮是不是版本問(wèn)題,但實(shí)際5.7和8.0是針對(duì)此項(xiàng)毫無(wú)分別的
It is also possible to have more than one nonaggregate column in the SELECT list when employing ONLY_FULL_GROUP_BY. In this case, every such column must be limited to a single value in the WHERE clause, and all such limiting conditions must be joined by logical AND, as shown here:
在改變sql_mode的情況下,在WHERE子句中,每一個(gè)這樣的列必須限制為一個(gè)值,并且所有這樣的限制條件必須由邏輯and連接
正常的sql語(yǔ)句還是select與group by后的字段一致,或者后面跟隨函數(shù)操作。官網(wǎng)知識(shí)提到了sql版本高了語(yǔ)法不會(huì)報(bào)錯(cuò),在改變sql_mode的情況下,此語(yǔ)法錯(cuò)誤,官網(wǎng)第一句話
SQL-92 and earlier does not permit queries for which the select list, HAVING
condition, or ORDER BY
list refer to nonaggregated columns that are not named in the GROUP BY
clause. For example, this query is illegal in standard SQL-92 because the nonaggregated name
column in the select list does not appear in the GROUP BY
:
示例:
SELECT o.custid, c.name, MAX(o.payment) FROM orders AS o, customers AS c WHERE o.custid = c.custid GROUP BY o.custid;
group by 的是custid,但是select字段除函數(shù)操作外多了個(gè)name,this query is illegal,但只強(qiáng)調(diào)SQL-92版本,我以為是版本問(wèn)題,但是SQL8.0與5.7一模一樣啊,并沒(méi)有找到高版本怎么解決這個(gè)問(wèn)題
說(shuō)起來(lái)這個(gè)sql_mode吧,我以前的例子提到過(guò)改變sql_mode,再執(zhí)行語(yǔ)法是報(bào)錯(cuò)的
設(shè)定sql模式 set sql_mode='ONLY_FULL_GROUP_BY';
但其實(shí)這樣還是不能證明這個(gè)問(wèn)題,因?yàn)楦膭?dòng)了sql_mode,
群里也討論了
直到現(xiàn)在我還是不能理解此語(yǔ)法問(wèn)題,只能說(shuō)特殊場(chǎng)景會(huì)出現(xiàn)數(shù)據(jù)結(jié)果不準(zhǔn)確問(wèn)題,網(wǎng)上說(shuō),mysql進(jìn)行了優(yōu)化,沒(méi)有g(shù)roup by的字段會(huì)隨機(jī)返回一條,如果語(yǔ)義無(wú)錯(cuò),就只能是邏輯錯(cuò)誤了,所以難道我一開(kāi)始舉的例子并不正確?
以上就是group by到底是什么的全部?jī)?nèi)容了,更多與group by到底是什么相關(guān)的內(nèi)容可以搜索億速云之前的文章或者瀏覽下面的文章進(jìn)行學(xué)習(xí)哈!相信小編會(huì)給大家增添更多知識(shí),希望大家能夠支持一下億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。