sqlÖÐsumº¯ÊýÔõôÓÃ
sqlÖеÄsum()º¯ÊýÓÃÓÚÅÌËãÊý×ÖÁеÄ×ܺ͡£Ëü¿ÉÒÔƾָ֤¶¨ÁС¢¹ýÂËÌõ¼þ¡¢ÓÖÃû¡¢·Ö×éºÍ¾ÛºÏ¶à¸öÁÐÀ´ÅÌËã×ܺͣ¬µ«½ö´¦ÀíÊý×ÖÖµ£¬ºöÂÔnullÖµ¡£
SQL ÖÐ SUM() º¯ÊýµÄʹÓÃ
SQL ÖÐµÄ SUM() º¯ÊýÓÃÓÚÅÌËãÒ»×éÊý×ÖµÄ×ܺ͡£ËüÊÇÒ»¸ö¾ÛºÏº¯Êý£¬¿ÉÒÔ¶ÔÒ»×éÐлò±í¾ÙÐвÙ×÷¡£
Óï·¨£º
SUM(expression)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
expression£ºÒªÇóºÍµÄÊý×Ö±í´ïʽ»òÁС£
ʹÓ÷½·¨£º
Ö¸¶¨ÇóºÍÁУºÖ¸¶¨ÒªÅÌËã×ܺ͵ÄÌض¨ÁС£ÀýÈ磺
SELECT SUM(salary) FROM employees;
µÇ¼ºó¸´ÖÆ
¹ýÂËЧ¹û£ºÊ¹Óà WHERE ×Ó¾ä¹ýÂËÊý¾Ý£¬½öÅÌËãÖª×ãÌض¨Ìõ¼þµÄÐС£ÀýÈ磺
SELECT SUM(salary) FROM employees WHERE department = 'Sales';
µÇ¼ºó¸´ÖÆ
ÓÖÃû£ºÎª SUM() º¯ÊýµÄÅÌËãЧ¹ûÖ¸¶¨Ò»¸öÌåÃû£¬ÒÔÀû±ãʹÓá£ÀýÈ磺
SELECT SUM(salary) AS total_salary FROM employees;
µÇ¼ºó¸´ÖÆ
·Ö×飺ʹÓà GROUP BY ×Ӿ佫Êý¾Ý·Ö×飬Ȼºó¶Ôÿ¸ö×éÅÌËã×ܺ͡£ÀýÈ磺
SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department;
µÇ¼ºó¸´ÖÆ
¾ÛºÏ¶à¸öÁУºSUM() º¯Êý¿ÉÒÔͬʱ¶Ô¶à¸öÁÐÅÌËã×ܺ͡£ÀýÈ磺
SELECT SUM(salary) + SUM(bonus) AS total_compensation FROM employees;
µÇ¼ºó¸´ÖÆ
×¢ÖØÊÂÏ
SUM() º¯Êý½ö¶ÔÊý×ÖÖµÓÐÓá£
ÈôÊÇÒªÅÌËãµÄÁаüÀ¨ NULL Öµ£¬Ôò NULL Öµ½«±»ºöÂÔ¡£
SUM() º¯Êý¿ÉÒÔÓëÆäËû¾ÛºÏº¯Êý£¨ÀýÈç MIN()¡¢MAX()¡¢COUNT()£©Ò»ÆðʹÓá£
ÒÔÉϾÍÊÇsqlÖÐsumº¯ÊýÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡