sqlÖÐgroupbyµÄ×÷ÓÃ
sql group by ×Ó¾äͨ¹ý°´ÁзÖ×é²¢¾ÛºÏÊý¾ÝÀ´×éÖ¯Êý¾Ý¼¯¡£ËüÓÃÓÚ£º½«±í°´Ö¸¶¨ÁзÖ×éÓ¦ÓþۺϺ¯Êý£¨Èç sum¡¢avg¡¢count£©ÅÌËã×éÖµ
SQL ÖÐ GROUP BY ×Ó¾ä
GROUP BY ×Ó¾äÊÇ SQL ÖÐÒ»¸ö¹¦Ð§Ç¿Ê¢µÄ¹¤¾ß£¬ËüÔÊÐíÓû§½«Êý¾Ý¼¯°´Ò»¸ö»ò¶à¸öÁзÖ×飬²¢¶Ôÿ¸ö×é¾ÛºÏÊý¾Ý¡£
×÷ÓÃ
GROUP BY ×Ó¾äµÄÖ÷Òª×÷ÓÃÈçÏ£º
½«Êý¾Ý¼¯Æ¾Ö¤Ö¸¶¨µÄÁзÖ×é
¶Ôÿ¸ö×é¾ÛºÏÊý¾Ý£¬ÈçÅÌËã×ܺ͡¢Æ½¾ùÖµ»ò¼ÆÊý
Óï·¨
GROUP BY ×Ó¾äµÄÓï·¨ÈçÏ£º
SELECT ÁÐÃû1, ÁÐÃû2, ... FROM ±íÃû GROUP BY ÁÐÃû3, ÁÐÃû4, ...
µÇ¼ºó¸´ÖÆ
ÁÐÃû1, ÁÐÃû2, …£ºÒªÑ¡ÔñµÄÁÐ
±íÃû£ºÒªÅÌÎʵıí
ÁÐÃû3, ÁÐÃû4, …£ºÒª·Ö×éµÄÁÐ
ʹÓÃʾÀý
ÒÔÏÂÊÇÒ»¸öʹÓà GROUP BY ×Ó¾äµÄʾÀý£º
SELECT department, SUM(salary) FROM employees GROUP BY department
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ« employees ±í°´ department ÁзÖ×飬²¢ÅÌËãÿ¸ö²¿·ÖµÄ×ÜÈËΪ¡£
¾ÛºÏº¯Êý
GROUP BY ×Ó¾äͨ³£Óë¾ÛºÏº¯ÊýÒ»ÆðʹÓã¬ÕâЩº¯Êý¶Ôÿ¸ö×éÅÌËãÖµ¡£Ò»Ð©³£¼ûµÄ¾ÛºÏº¯Êý°üÀ¨£º
SUM()£ºÅÌËã×ܺÍ
AVG()£ºÅÌËãƽ¾ùÖµ
COUNT()£ºÅÌËã¼ÆÊý
MIN()£ºÅÌËã×îСֵ
MAX()£ºÅÌËã×î´óÖµ
¶àÁзÖ×é
GROUP BY ×Ó¾ä¿ÉÒÔͬʱ°´¶à¸öÁзÖ×é¡£ÀýÈ磺
SELECT department, location, SUM(salary) FROM employees GROUP BY department, location
µÇ¼ºó¸´ÖÆ
´ËÅÌÎʽ« employees ±í°´ department ºÍ location ÁзÖ×飬²¢ÅÌËãÿ¸ö²¿·ÖºÍλÖõÄ×ÜÈËΪ¡£
ÒÔÉϾÍÊÇsqlÖÐgroupbyµÄ×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡