真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

MySQL8.0新特性--Groupby-創(chuàng)新互聯(lián)

Group by 語(yǔ)句用于結(jié)合聚合函數(shù)(如count,sum,avg,max,min),根據(jù)一個(gè)或多個(gè)列對(duì)結(jié)果集進(jìn)行分組。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到上猶網(wǎng)站設(shè)計(jì)與上猶網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋上猶地區(qū)。

(1)去掉重復(fù)值:根據(jù)group by后面的關(guān)鍵字只顯示一行結(jié)果;

(2)mysql5.7默認(rèn)開(kāi)啟參數(shù)ONLY_FULL_GROUP_BY,表示完全group by,即select后面跟的列g(shù)roup by后面也必須有,但是group by后面跟的列,select后面不一定需要出現(xiàn);

mysql> select @@version; +-----------+ | @@version | +-----------+ | 8.0.13    | +-----------+ 1 row in set (0.00 sec) mysql> show variables like '%sql_mode%'; +---------------+-----------------------------------------------------------------------------------------------------------------------+ | Variable_name | Value                                                                                                                 | +---------------+-----------------------------------------------------------------------------------------------------------------------+ | sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | +---------------+-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) mysql> select * from t_group; +--------+---------+------------+------------+ | emp_no | dept_no | from_date  | to_date    | +--------+---------+------------+------------+ |  22744 | d006    | 1986-12-01 | 9999-01-01 | |  24007 | d005    | 1986-12-01 | 9999-01-01 | |  30970 | d005    | 1986-12-01 | 2017-03-29 | |  31112 | d002    | 1986-12-01 | 1993-12-10 | |  40983 | d005    | 1986-12-01 | 9999-01-01 | |  46554 | d008    | 1986-12-01 | 1992-05-27 | |  48317 | d008    | 1986-12-01 | 1989-01-11 | |  49667 | d007    | 1986-12-01 | 9999-01-01 | |  50449 | d005    | 1986-12-01 | 9999-01-01 | |  10004 | d004    | 1986-12-01 | 9999-01-01 | +--------+---------+------------+------------+ 10 rows in set (0.00 sec) mysql> select dept_no,count(*) from t_group group by dept_no; +---------+----------+ | dept_no | count(*) | +---------+----------+ | d006    |        1 | | d005    |        4 | | d002    |        1 | | d008    |        2 | | d007    |        1 | | d004    |        1 | +---------+----------+ 6 rows in set (0.00 sec) mysql> select dept_no,emp_no,count(*) from t_group group by dept_no; ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'employees.t_group.emp_no' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 關(guān)閉ONLY_FULL_GROUP_BY參數(shù)后,不報(bào)錯(cuò),但是結(jié)果是不完全group by; mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; Query OK, 0 rows affected (0.01 sec) mysql> select dept_no,emp_no,count(*) from t_group group by dept_no; +---------+--------+----------+ | dept_no | emp_no | count(*) | +---------+--------+----------+ | d006    |  22744 |        1 | | d005    |  24007 |        4 | | d002    |  31112 |        1 | | d008    |  46554 |        2 | | d007    |  49667 |        1 | | d004    |  10004 |        1 | +---------+--------+----------+ 6 rows in set (0.00 sec)

(3)mysql5.7group by 默認(rèn)還有排序功能,8.0默認(rèn)只分組不排序,需要加order by才排序,這點(diǎn)可以從執(zhí)行結(jié)果是否有Using filesort來(lái)判斷

mysql> select @@version; +-----------+ | @@version | +-----------+ | 8.0.13    | +-----------+ 1 row in set (0.00 sec) mysql> select dept_no,count(*) from t_group group by dept_no; +---------+----------+ | dept_no | count(*) | +---------+----------+ | d006    |        1 | | d005    |        4 | | d002    |        1 | | d008    |        2 | | d007    |        1 | | d004    |        1 | +---------+----------+ 6 rows in set (0.00 sec) mysql> desc select dept_no,count(*) from t_group group by dept_no; +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-----------------+ |  1 | SIMPLE      | t_group | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | Using temporary | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 1 row in set, 1 warning (0.00 sec) root@localhost [testdb]>select @@version; +------------+ | @@version  | +------------+ | 5.7.16-log | +------------+ 1 row in set (0.00 sec) root@localhost [testdb]>select dept_no,count(*) from t_group group by dept_no; +---------+----------+ | dept_no | count(*) | +---------+----------+ | d002    |        1 | | d004    |        1 | | d005    |        4 | | d006    |        1 | | d007    |        1 | | d008    |        2 | +---------+----------+ 6 rows in set (0.00 sec) root@localhost [testdb]>desc select dept_no,count(*) from t_group group by dept_no; +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+ | id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                           | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+ |  1 | SIMPLE      | t_group | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | Using temporary; Using filesort | +----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+---------------------------------+ 1 row in set, 1 warning (0.00 sec)

(4) group by是否能排序會(huì)直接影響分頁(yè)查詢結(jié)果

8.0.13版本 mysql> select dept_no,count(*) from t_group group by dept_no limit 1; +---------+----------+ | dept_no | count(*) | +---------+----------+ | d006    |        1 | +---------+----------+ 1 row in set (0.01 sec) 5.7.16版本: root@localhost [testdb]>select dept_no,count(*) from t_group group by dept_no limit 1; +---------+----------+ | dept_no | count(*) | +---------+----------+ | d002    |        1 | +---------+----------+ 1 row in set (0.00 sec)

參考鏈接

8.2.1.15 GROUP BY Optimization

MySQL 5.7有關(guān)group by說(shuō)明的片段如下:

In MySQL, GROUP BY is used for sorting, so the server may also apply ORDER BY optimizations to grouping. However, relying on implicit or explicit GROUP BY sorting is deprecated. See Section 8.2.1.14, “ORDER BY Optimization”.

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


文章標(biāo)題:MySQL8.0新特性--Groupby-創(chuàng)新互聯(lián)
分享鏈接:http://weahome.cn/article/dppohd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部