sqlÖÐgroup byÊÇʲôÒâ˼
group by ÊÇ sql ÖеľۺϺ¯Êý£¬ÓÃÓÚƾָ֤¶¨ÁжÔÊý¾Ý·Ö×é²¢Ö´ÐоۺϲÙ×÷¡£ËüÔÊÐíÓû§£ºÆ¾Ö¤Ìض¨ÁÐÖµ¶ÔÊý¾ÝÐоÙÐзÖ×é¡£¶Ôÿ¸ö×éÓ¦ÓþۺϺ¯Êý£¨ÈçÇóºÍ¡¢¼ÆÊý¡¢Çóƽ¾ùÖµ£©¡£´ÓÖØ´óÊý¾Ý¼¯Öн¨ÉèÓÐÒâÒåµÄÕªÒª£¬Ö´ÐÐÊý¾Ý¾ÛºÏºÍ·Ö×é¡£
SQL ÖÐµÄ GROUP BY
GROUP BY ÊÇ SQL ÖÐÒ»¸öÖ÷ÒªµÄ¾ÛºÏº¯Êý£¬ËüÔÊÐíÓû§Æ¾Ö¤Ö¸¶¨ÁжÔÊý¾Ý¾ÙÐзÖ×飬Ȼºó¶Ôÿ¸ö×éÖ´ÐоۺϲÙ×÷£¨Èç SUM¡¢COUNT¡¢AVG µÈ£©¡£
ÔõÑùʹÓà GROUP BY£¿
GROUP BY ×Ó¾äÓÃÓÚ SELECT Óï¾äÖУ¬Æä»ù±¾Óï·¨ÈçÏ£º
SELECT ¾ÛºÏº¯Êý(ÁÐÃû) FROM ±íÃû GROUP BY ÁÐÃû
µÇ¼ºó¸´ÖÆ
ÀýÈ磬Ҫƾ֤ ²¿·Ö ÁÐ¶Ô Ô±¹¤ ±íÖеÄÊý¾Ý¾ÙÐзÖ×é²¢ÅÌËãÿ¸ö²¿·ÖµÄÔ±¹¤×ÜÊý£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT COUNT(employee_id) FROM employees GROUP BY department_id
µÇ¼ºó¸´ÖÆ
GROUP BY µÄÊÂÇéÔÀí
GROUP BY »á½«Êý¾Ý±íÖеÄÐÐƾָ֤¶¨µÄÁÐÖµ·Ö×é¡£Ëü½«¾ßÓÐÏàͬ·Ö×éÁÐÖµµÄÐÐ×éºÏµ½Ò»Æð£¬ÐγÉÒ»¸ö×顣Ȼºó£¬Ëü¶Ôÿ¸ö×éÓ¦ÓÃÖ¸¶¨µÄ¾ÛºÏº¯Êý£¬ÌìÉúÒ»¸ö¾ÛºÏЧ¹û¡£
GROUP BY µÄÓŵã
Êý¾ÝÕªÒª£ºGROUP BY ¿ÉÓÃÓÚ´Ó´óÐÍÊý¾Ý¼¯¿ìËÙ½¨ÉèÓÐÒâÒåµÄÕªÒª¡£
Êý¾Ý¾ÛºÏ£ºËüÔÊÐíÓû§¶Ô·Ö×éÊý¾ÝÖ´ÐÐÖÖÖ־ۺϲÙ×÷£¬ÀýÈçÅÌËã×ܺ͡¢Æ½¾ùÖµ»ò¼ÆÊý¡£
Êý¾Ý·Ö×飺GROUP BY ¿ÉÓÃÓÚ½«Êý¾Ý»®·ÖΪ²î±ðµÄÖÖ±ð»ò×飬ÒÔÀû±ãÆÊÎöºÍ¿ÉÊÓ»¯¡£
ÒÔÉϾÍÊÇsqlÖÐgroup byÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡