×ðÁú¿­Ê±

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µÄ×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿