mysqlÖÐgroup byÊÇʲôÒâ˼
group by ÊÇÒ»¸ö sql ¾ÛºÏº¯Êý£¬ÓÃÓÚ°´Ö¸¶¨ÁжÔÊý¾ÝÐоÙÐзÖ×飬²¢¶Ôÿ¸ö×éÖ´ÐÐÅÌËã¡£ÆäÓ﷨Ϊ£ºselect ÁÐÃû, ¾ÛºÏº¯Êý(ÁÐÃû) from ±íÃû group by ÁÐÃû; ËüÔÊÐíÊý¾Ý»ã×Ü¡¢¾ÛºÏºÍͳ¼ÆÆÊÎö£¬²¢¿ÉÓÅ»¯ÅÌÎÊÐÔÄÜ¡£
MySQL ÖÐµÄ GROUP BY
GROUP BY ÊÇʲô£¿
GROUP BY ÊÇÒ»ÖÖ SQL ¾ÛºÏº¯Êý£¬ÓÃÓÚ½«¾ßÓÐÏàֵͬµÄÊý¾ÝÐзÖ×飬²¢¶Ôÿ¸ö×éÖ´ÐоۺÏÅÌËã¡£
ÔõÑùʹÓà GROUP BY£¿
GROUP BY ×Ó¾äÔÚ SELECT Óï¾äÖÐʹÓã¬ÓÃÓÚÖ¸¶¨·Ö×éÒÀ¾ÝµÄÁС£ÆäÓï·¨ÈçÏ£º
SELECT ÁÐÃû, ¾ÛºÏº¯Êý(ÁÐÃû) FROM ±íÃû GROUP BY ÁÐÃû;
µÇ¼ºó¸´ÖÆ
ʾÀý£º
ÀýÈ磬ÒÔÏÂÅÌÎʽ«°´ country ×Ö¶Î¶Ô customers ±í¾ÙÐзÖ×飬²¢ÅÌËãÿ¸ö¹ú¼Ò¿Í»§×ÜÊý£º
SELECT country, COUNT(*) AS total_customers FROM customers GROUP BY country;
µÇ¼ºó¸´ÖÆ
GROUP BY µÄÀûÒ棺
Êý¾Ý»ã×Ü£ºGROUP BY ÔÊÐíÄú½«´óÐÍÊý¾Ý¼¯»ã×ÜΪ¸üÒ×ÓÚÖÎÀíºÍÃ÷È·µÄÕªÒª¡£
Êý¾Ý¾ÛºÏ£ºÍ¨¹ýʹÓþۺϺ¯Êý£¬Èç SUM¡¢COUNT ºÍ AVERAGE£¬¿ÉÒÔÖ´ÐÐÖÖÖÖÊý¾Ý¾ÛºÏ¡£
ͳ¼ÆÆÊÎö£ºGROUP BY ¿ÉÓÃÓÚ¾ÙÐÐͳ¼ÆÆÊÎö£¬ÀýÈç²éÕÒ×î³£¼ûµÄÏîÄ¿»òÅÌËãƽ¾ùÖµ¡£
ÐÔÄÜÓÅ»¯£º¾ÛºÏÅÌÎÊͨ³£±È´¦Àíδ·Ö×éÊý¾ÝµÄÅÌÎʸü¿ì£¬ÓÉÓÚËüÃÇÏÔ×ÅïÔÌÁËЧ¹û¼¯µÄ¾Þϸ¡£
×¢ÖØÊÂÏ
GROUP BY ×Ó¾äÖ¸¶¨µÄÁбØÐè·ºÆðÔÚ SELECT ÁбíÖлò×÷Ϊ¾ÛºÏº¯ÊýµÄ²ÎÊý¡£
GROUP BY ×Ó¾äÖеÄÁбØÐèÊÇÏàͬµÄÀàÐÍ¡£
ÈôÊÇ·Ö×éÒÀ¾ÝµÄÁаüÀ¨ NULL Öµ£¬Ôò NULL Öµ½«ÐγÉÒ»¸öµ¥¶ÀµÄ×é¡£
ÒÔÉϾÍÊÇmysqlÖÐgroup byÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡