sqlÖÐsumµÄÓ÷¨½â˵
sql ÖÐµÄ sum º¯ÊýÓÃÓÚÅÌËãÒ»×éÖµµÄ×ܺͣ¬Ó﷨Ϊ sum(expression)¡£Ëüͨ³£Óë where ×Ó¾äÍŽáʹÓã¬ÒÔ¶ÔÌض¨Ìõ¼þϵÄÖµ¾ÙÐÐÇóºÍ¡£sum º¯Êý¿ÉÒÔÓë distinct Òªº¦×ÖÍŽáʹÓã¬ÒÔ¶ÔΨһֵ¾ÙÐÐÇóºÍ£¬Ò²¿ÉÒÔÓë group by ×Ó¾äÍŽáʹÓã¬ÒÔ¶Ô·Ö×éºóµÄÊý¾Ý¾ÙÐÐÇóºÍ¡£
SQL ÖÐ SUM º¯Êý
SUM º¯ÊýÊÇ SQL Öг£ÓõľۺϺ¯Êý£¬ÓÃÓÚÅÌËãÒ»×éÖµµÄ×ܺ͡£Ëüͨ³£ÓÃÓÚͳ¼ÆÆÊÎöºÍÊý¾Ý»ã×Ü¡£
Óï·¨
SUM(expression)
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬expression ÊÇÒªÅÌËã×ܺ͵ÄÁлò±í´ïʽ¡£
ʾÀý
ÒÔÏÂÅÌÎÊÅÌËã±í customer_orders ÖÐËùÓж©µ¥½ð¶îµÄ×ܺͣº
SELECT SUM(order_amount) FROM customer_orders;
µÇ¼ºó¸´ÖÆ
Ó÷¨
SUM º¯Êýͨ³£Óë WHERE ×Ó¾äÍŽáʹÓã¬ÒÔ¶ÔÌض¨Ìõ¼þϵÄÖµ¾ÙÐÐÇóºÍ¡£ÀýÈ磬ÒÔÏÂÅÌÎÊÅÌËãËùÓÐ×ܶ©µ¥½ð¶îÁè¼Ý 100 ÃÀÔªµÄ¶©µ¥µÄ×ܺͣº
SELECT SUM(order_amount) FROM customer_orders WHERE order_amount > 100;
µÇ¼ºó¸´ÖÆ
×¢ÖØÊÂÏî
SUM º¯Êý²»¿ÉÓÃÓÚÇóºÍ NULL Öµ¡£ÈôÊDZ£´æ NULL Öµ£¬Ôò»áºöÂÔËüÃÇ¡£
SUM º¯Êý¿ÉÒÔÓë DISTINCT Òªº¦×ÖÍŽáʹÓã¬ÒÔ¶ÔΨһֵ¾ÙÐÐÇóºÍ¡£
SUM º¯Êý¿ÉÒÔÓë GROUP BY ×Ó¾äÍŽáʹÓã¬ÒÔ¶Ô·Ö×éºóµÄÊý¾Ý¾ÙÐÐÇóºÍ¡£
ÒÔÉϾÍÊÇsqlÖÐsumµÄÓ÷¨½â˵µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡