mysqlÖзÖ×麯ÊýÔõôд
mysql ÖеķÖ×麯ÊýÓÃÓÚ½«Êý¾Ý¼¯°´·Ö×éÅÌËã¾ÛºÏÖµ¡£³£Óõĺ¯ÊýÓУºsum£ºÅÌËãÖ¸¶¨ÁÐÖÐÖµµÄ×ܺÍcount£ºÅÌËãÖ¸¶¨ÁÐÖÐ·Ç null ÖµµÄÊýÄ¿avg£ºÅÌËãÖ¸¶¨ÁÐÖÐÖµµÄƽ¾ùÖµmin£ºÅÌËãÖ¸¶¨ÁÐÖеÄ×îСֵmax£ºÅÌËãÖ¸¶¨ÁÐÖеÄ×î´óÖµ
MySQL ·Ö×麯Êý
MySQL ÖеķÖ×麯ÊýÓÃÓÚƾ֤һ×é»ò¶à×éÁжÔÊý¾Ý¼¯¾ÙÐзÖ×飬²¢Õë¶Ôÿ×éÅÌËã¾ÛºÏÖµ£¨ÀýÈç SUM¡¢COUNT¡¢AVG£©¡£ÒÔÏÂÊdz£Ó÷Ö×麯ÊýµÄÓï·¨£º
SELECT ÁÐ1, ÁÐ2, ¾ÛºÏº¯Êý(ÁÐ3) FROM ±íÃû GROUP BY ÁÐ1, ÁÐ2
µÇ¼ºó¸´ÖÆ
³£Ó÷Ö×麯Êý
SUM(ÁÐÃû)£ºÅÌËãÖ¸¶¨ÁÐÖÐÖµµÄ×ܺ͡£
COUNT(ÁÐÃû)£ºÅÌËãÖ¸¶¨ÁÐÖÐ·Ç NULL ÖµµÄÊýÄ¿¡£
AVG(ÁÐÃû)£ºÅÌËãÖ¸¶¨ÁÐÖÐÖµµÄƽ¾ùÖµ¡£
MIN(ÁÐÃû)£ºÅÌËãÖ¸¶¨ÁÐÖеÄ×îСֵ¡£
MAX(ÁÐÃû)£ºÅÌËãÖ¸¶¨ÁÐÖеÄ×î´óÖµ¡£
·Ö×麯ÊýʹÓÃʾÀý
ÒÔÏÂʾÀýÑÝʾÔõÑùʹÓ÷Ö×麯ÊýÅÌËãÏúÊۼͼÖÐÿÖÖ²úÆ·µÄ×ÜÏúÊ۶
SELECT product_id, SUM(quantity_sold) AS total_sales FROM sales_records GROUP BY product_id;
µÇ¼ºó¸´ÖÆ
Ч¹û½«ÏÔʾÿÖÖ²úÆ·µÄ product_id ºÍ×ÜÏúÊ۶
×¢ÖØÊÂÏî
·Ö×麯ÊýÖ»ÄÜÓÃÓÚ GROUP BY ×Ó¾äÖÐÖ¸¶¨µÄÁС£
ÈôÊÇÔÚ GROUP BY ×Ó¾äÖÐÖ¸¶¨Á˶à¸öÁУ¬Ôò¾ÛºÏº¯ÊýÖ»ÄÜÓ¦ÓÃÓÚÄÇЩÁлòËüÃǵıí´ïʽµÄ×Ó¼¯ÖС£
·Ö×麯Êý²»¿ÉÓÃÓÚ×ÓÅÌÎÊ¡£
ÔÚʹÓ÷Ö×麯Êýʱ£¬ÐèҪעÖØ NULL ÖµµÄ´¦Öóͷ£·½·¨¡£ÈôÊÇÖ¸¶¨ÁÐÖб£´æ NULL Öµ£¬Ôò¸ÃÖµ½«´ÓÅÌËãÖÐɨ³ý¡£
ÒÔÉϾÍÊÇmysqlÖзÖ×麯ÊýÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡