sqlÖÐgroup byµÄ×÷ÓÃ
sql ÖÐµÄ group by ×Ó¾äÓÃÓÚ·Ö×éÊý¾Ý²¢ÅÌËã¾ÛºÏÖµ£º°´Ö¸¶¨Áлò±í´ïʽ·Ö×éÊý¾Ý¡£ÅÌËãÿ¸ö×éµÄ¾ÛºÏÖµ£¨Èç sum¡¢count¡¢min¡¢max µÈ£©¡£ïÔÌÊý¾Ý¼¯¾Þϸ£¬¸üÒ×ÓÚ´¦Öóͷ£ºÍÆÊÎö¡£
GROUP BY ÓÃ;
SQL ÖÐµÄ GROUP BY ×Ó¾äÓÃÓÚ½«Êý¾Ý¼¯°´Ö¸¶¨Áлò±í´ïʽ¾ÙÐзÖ×飬²¢»ùÓÚÿ¸ö×éÅÌËã¾ÛºÏÖµ£¨Èç SUM¡¢COUNT¡¢MIN¡¢MAX µÈ£©¡£
×÷ÓÃ
GROUP BY µÄÖ÷Òª×÷ÓÃÈçÏ£º
¾ÛºÏÊý¾Ý£º½«Êý¾Ý°´×é¾ÙÐоۺϣ¬ÌìÉú»ã×ÜÐÅÏ¢¡£ÀýÈ磬ÅÌËãÿ¸ö¿Í»§µÄ¶©µ¥×ܶî»òÿ¸ö²¿·ÖµÄÔ±¹¤×ÜÊý¡£
·Ö×éÐУº°´Ö¸¶¨Áлò±í´ïʽ¶ÔÊý¾Ý¾ÙÐзÖ×飬½«¾ßÓÐÏàͬ×éÖµµÄÐÐ×éºÏÔÚÒ»Æð¡£ÕâʹµÃ¸üÇáËɵØʶ±ðºÍÆÊÎö²î±ð×éÖ®¼äµÄģʽºÍ²î±ð¡£
ïÔÌÊý¾Ý¼¯´óС£ºÍ¨Ì«¹ý×éºÍ¾ÛºÏ£¬GROUP BY ¿ÉÒÔÏÔÖøïÔÌÊý¾Ý¼¯µÄ¾Þϸ£¬Ê¹Æä¸üÒ×ÓÚ´¦Öóͷ£ºÍÆÊÎö¡£
Óï·¨
GROUP BY ×Ó¾äµÄÓï·¨ÈçÏ£º
SELECT aggregation_function(column_name) FROM table_name GROUP BY column_name;
µÇ¼ºó¸´ÖÆ
ʾÀý
ÀýÈ磬ÒÔÏÂÅÌÎÊ°´¿Í»§·Ö×é²¢ÅÌËãÿ¸ö¿Í»§µÄ¶©µ¥ÊýÄ¿£º
SELECT COUNT(*), customer_name FROM orders GROUP BY customer_name;
µÇ¼ºó¸´ÖÆ
Õâ¸öÅÌÎʽ«·µ»ØÒ»¸öЧ¹û¼¯£¬ÆäÖÐÿһÐÐÌåÏÖÒ»¸öÆæÒìµÄ¿Í»§£¬ÒÔ¼°ËûÃǵĶ©µ¥ÊýÄ¿¡£
×¢ÖØÊÂÏî
GROUP BY ×Ó¾ä±ØÐè·ºÆðÔھۺϺ¯Êý֮ǰ¡£
Ö»ÄÜ°´ÔÚ SELECT Áбí»ò HAVING ×Ó¾äÖÐÒýÓõÄÁлò±í´ïʽ¾ÙÐзÖ×é¡£
ÈôÊÇÔÚ GROUP BY ×Ó¾äÖаüÀ¨µÄÁаüÀ¨ NULL Öµ£¬Ôò»áÔÚ·Ö×éÖн¨Éèµ¥¶ÀµÄ×é¡£
ÒÔÉϾÍÊÇsqlÖÐgroup byµÄ×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡