mysqlÖÐavgµÄÓ÷¨
mysql µÄ avg() º¯ÊýÓÃÓÚÅÌËãÊý×ÖÖµµÄƽ¾ùÖµ¡£ËüÖ§³Ö¶àÖÖÓ÷¨£¬°üÀ¨£ºÅÌËãËùÓÐÏúÊÛ²úÆ·µÄƽ¾ùÊýÄ¿£ºselect avg(quantity_sold) from sales;ÅÌËã¼ÛÇ®µÄƽ¾ùÖµ£ºavg(price);ÅÌËãÏúÊÛ¶îµÄƽ¾ùÖµ£ºavg(quantity_sold * price)¡£avg() º¯ÊýºöÂÔ null Öµ£¬Ê¹Óà ifnull() ¿ÉÅÌËã·Ç¿ÕÖµµÄƽ¾ùÖµ¡£
MySQL ÖÐ AVG() º¯ÊýµÄÓ÷¨
AVG() º¯ÊýÓÃÓÚÅÌËãÒ»×éÊý×ÖÖµµÄƽ¾ùÖµ¡£ËüÊÇÒ»¸ö¾ÛºÏº¯Êý£¬ÕâÒâζ×ÅËü½«Ò»×éÐÐÖеÄÖµ»ã×ÜΪµ¥¸öЧ¹û¡£
Óï·¨£º
AVG(expression)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
expression ÊÇÒªÅÌËãƽ¾ùÖµµÄÄ¿µÄÁлò±í´ïʽ¡£
Ó÷¨Ê¾Àý£º
¼ÙÉèÎÒÃÇÓÐÒ»ÕÅÃûΪ “sales” µÄ±í£¬ÆäÖаüÀ¨ÒÔÏÂÁУº
id (Ö÷¼ü)
product_name
quantity_sold
price
ÒªÅÌËãËùÓÐÏúÊÛ²úÆ·µÄƽ¾ùÊýÄ¿£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT AVG(quantity_sold) FROM sales;
µÇ¼ºó¸´ÖÆ
Ч¹û½«ÊÇÒ»¸ö°üÀ¨Æ½¾ùÊýÄ¿ÖµµÄµ¥¸öÐС£
ÆäËûÓÃ;£º
AVG() º¯Êý»¹¿ÉÒÔÓÃÓÚÅÌËãÆäËûÀàÐ͵Äƽ¾ùÖµ£¬ÀýÈ磺
ƽ¾ù¼ÛÇ®£ºAVG(price)
ƽ¾ùÏúÊ۶AVG(quantity_sold * price)
×¢ÖØÊÂÏ
AVG() º¯ÊýºöÂÔ NULL Öµ¡£
ÈôÊÇ expression ÖÐÖ»°üÀ¨ NULL Öµ£¬Ôò AVG() º¯Êý½«·µ»Ø NULL¡£
ÒªÅÌËã·Ç¿ÕÖµµÄƽ¾ùÖµ£¬¿ÉÒÔʹÓà AVG(IFNULL(expression, 0))¡£
ÒÔÉϾÍÊÇmysqlÖÐavgµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡