sqlÖÐgroup byʲôÒâ˼
group by ×Ó¾äÓÃÓÚ½«Êý¾Ý·Ö×飬²¢Æ¾Ö¤×é±ð¾ÙÐоۺϲÙ×÷¡£Ëü½«¾ßÓÐÏàֵͬµÄÐеÄ×é·Ö×éÔÚÒ»Æ𣬲¢ÔÚÿ¸ö×éÄÚÅÌËã»ã×ÜÖµ»òɸѡÊý¾Ý¡£
GROUP BY£ºÊý¾Ý·Ö×é
ÔÚ SQL ÖУ¬GROUP BY ×Ó¾äÓÃÓÚ½«Êý¾Ý·Ö×飬ÒÔ±ãƾ֤×é±ð¾ÙÐоۺϲÙ×÷¡£¼ò¶øÑÔÖ®£¬Ëü½«¾ßÓÐÏàֵͬµÄÒ»×éÐзÖ×éµ½Ò»Æð¡£
Ó÷¨
GROUP BY ×Ó¾ä½ôËæ SELECT ×Ӿ䣬ָ¶¨·Ö×éµÄÁлò±í´ïʽ¡£ÀýÈ磺
SELECT SUM(salary) FROM employee GROUP BY department;
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÅÌÎʽ«Ô±¹¤±íÖÐµÄ salary ÁзÖ×éµ½ department ÁÐÖС£Ã¿¸ö²¿·ÖµÄÈËΪ½«»®·ÖÇóºÍ¡£
ÀûÒæ
GROUP BY ÌṩÒÔÏÂÀûÒ棺
¾ÛºÏÊý¾Ý£ºÀýÈ磬ͨ¹ý SUM()¡¢COUNT()¡¢AVG() µÈ¾ÛºÏº¯ÊýÀ´ÅÌËã×éÄڵĻã×Ü¡£
ɸѡÊý¾Ý£ºÍ¨¹ý HAVING ×Ó¾äɸѡÌض¨×é¡£ÀýÈ磬ֻѡÔñÈËΪ×ܶî´óÓÚ 10000 µÄ²¿·Ö¡£
Ïû³ýÖظ´£º·Ö×é¿ÉÒÔÏû³ý¾ßÓÐÏàֵͬµÄÖظ´ÐС£
×¢ÖØÊÂÏî
GROUP BY ÁбØÐè·ºÆðÔÚ SELECT ×Ó¾äÖУ¬»òÕß±ØÐèÔÚ HAVING ×Ó¾äÖÐʹÓþۺϺ¯Êý¡£
ÈôÊDz»Ö¸¶¨ GROUP BY ÁУ¬Ôò½«¶ÔÕû¸ö±í¾ÙÐзÖ×飬´Ó¶ø±¬·¢µ¥¸ö×é¡£
ÒÔÉϾÍÊÇsqlÖÐgroup byʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡