sqlÖÐgroup byµÄÓ÷¨
sql ÖÐ group by ÓÃÓÚ·Ö×éÊý¾Ý¼¯²¢Ö´Ðлã×ܲÙ×÷¡£×é·Ö×éÁÐÓÃÓÚÖ¸¶¨·Ö×éÊý¾Ý¼¯µÄÁУ¬¶ø»ã×ܲÙ×÷Ö¸¶¨ÒªÖ´ÐеIJÙ×÷£¨ÀýÈçÇóºÍ»ò¼ÆÊý£©¡£Ê¾Àý£ºselect product_category, sum(quantity_sold) from sales_table group by product_category; ½«ÏúÁ¿°´²úÆ·ÖÖ±ð·Ö×é²¢ÅÌËãÿ¸öÀà±ðµÄ×ÜÏúÁ¿¡£
SQL ÖÐ GROUP BY µÄÓ÷¨
GROUP BY ×Ó¾äÓÃÓÚ½«Êý¾Ý¼¯·Ö×飬ÒÔ±ã¶Ôÿ¸ö×éÖ´Ðлã×ܲÙ×÷£¬ÀýÈçÇóºÍ¡¢Æ½¾ùÖµ»ò¼ÆÊý¡£
Óï·¨£º
SELECT column_list FROM table_name WHERE condition GROUP BY group_by_column_list
µÇ¼ºó¸´ÖÆ
Ó÷¨£º
Ö¸¶¨×é·Ö×éÁУ¨group_by_column_list£©£º´ËÁбíÖ¸¶¨ÓÃÓÚ·Ö×éÊý¾Ý¼¯µÄÁС£
Ö´Ðлã×ܲÙ×÷£¨column_list£©£º´ËÁбíÖ¸¶¨Òª¶Ôÿ¸ö×éÖ´ÐеĻã×ܲÙ×÷£¬ÀýÈ磺
SUM()£ºÇóºÍ
AVG()£ºÆ½¾ùÖµ
COUNT()£º¼ÆÊý
ʾÀý£º
ÅÌËãÿ¸ö²úÆ·Àà±ðµÄ×ÜÏúÁ¿£º
SELECT product_category, SUM(quantity_sold) FROM sales_table GROUP BY product_category;
µÇ¼ºó¸´ÖÆ
Êä³öЧ¹û½«·Ö×éÿ¸ö²úÆ·Öֱ𣬲¢ÏÔʾÿ¸öÀà±ðµÄ×ÜÏúÁ¿¡£
ÆäËû×¢ÖØÊÂÏ
¿É×éºÏ¶à¸ö×é·Ö×éÁУ¬ÀýÈ磺GROUP BY product_category, product_name¡£
»ã×ܲÙ×÷Ö»ÄÜÓ¦ÓÃÓÚ×é·Ö×éÁС£
ÈôÊÇÒª¶ÔËùÓÐÐÐÓ¦Óûã×ܲÙ×÷£¬¿ÉÒÔʹÓà GROUP BY 1£¬ÆäÖÐ 1 ÌåÏÖÊý¾Ý¼¯µÄµÚÒ»ÁС£
¿ÉÒÔÔÚ GROUP BY ×Ó¾äÖÐʹÓþۺϺ¯Êý£¬ÀýÈç MIN() ºÍ MAX()¡£
ÒÔÉϾÍÊÇsqlÖÐgroup byµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡