sqlÖÐsumʲôÒâ˼
sql ÖÐ sum µÄ¼ÄÒåÊÇÅÌËãÒ»×éÖµÖ®ºÍ£¬Ó﷨Ϊ sum(expression)¡£Ëü¿ÉÒÔÓ¦ÓÃÓÚÊý×ÖÁУ¬ºöÂÔ null Öµ£¬²¢¿ÉÓë group by ×Ó¾äÒ»ÆðʹÓã¬ÒÔÅÌËãÌض¨×éÖеÄÖµµÄ×ܺ͡£
SQL ÖÐ SUM µÄ¼ÄÒå
SUM ÊÇ SQL ÖÐÒ»¸ö¾ÛºÏº¯Êý£¬ÓÃÓÚÅÌËãÒ»×éÖµÖ®ºÍ¡£ËüµÄÓï·¨ÈçÏ£º
SUM(expression)
µÇ¼ºó¸´ÖÆ
ÆäÖÐ expression ÊÇÒªÅÌËãÆäºÍµÄÊýÖµ±í´ïʽ»òÁС£
Ó÷¨
SUM º¯Êý¿ÉÒÔÓ¦ÓÃÓÚÊý×ÖÁУ¬ÒÔ»ñµÃ¸ÃÁÐÖÐËùÓÐÖµµÄ×ܺ͡£ÀýÈ磺
SELECT SUM(salary) FROM employees;
µÇ¼ºó¸´ÖÆ
Õ⽫·µ»Ø employees ±íÖÐ salary ÁÐÖÐËùÓÐÖµµÄ×ܺ͡£
×¢ÖØÊÂÏî
SUM º¯ÊýºöÂÔ NULL Öµ¡£
ÈôÊÇ expression ÊÇÎı¾»òÈÕÆÚÀàÐÍ£¬SUM º¯Êý½«·µ»Ø NULL¡£
SUM º¯Êý¿ÉÒÔÓë GROUP BY ×Ó¾äÒ»ÆðʹÓã¬ÒÔÅÌËãÌض¨×éÖеÄÖµµÄ×ܺ͡£
ʾÀý
ÒÔÏÂʾÀýÑÝʾÔõÑùʹÓà SUM º¯ÊýÅÌËã²î±ð²¿·ÖµÄ×ÜÏúÊ۶
SELECT department, SUM(sales) AS total_sales FROM sales GROUP BY department;
µÇ¼ºó¸´ÖÆ
Õ⽫·µ»ØÒ»¸öЧ¹û¼¯£¬ÆäÖÐÿÐÐÏÔʾһ¸ö²¿·Ö¼°Æä×ÜÏúÊ۶
ÒÔÉϾÍÊÇsqlÖÐsumʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡