尊龙凯时人生就是搏

laravel sumÇóºÍ

laravelÊÇÒ»¿î±¸ÊܽӴýµÄphp¿ò¼Ü £¬ÌṩÁËÐí¶àÀû±ã¿ª·¢µÄ¹¦Ð§¡£ÆäÖÐ £¬¾Í°üÀ¨¹ØÓÚÊý¾Ý¿âµÄÅÌÎʺͻã×ܹ¦Ð§¡£±¾Îľͽ«×ÅÖؽâ˵ÔõÑùʹÓÃlaravelÖеÄsumÒªÁìÍê³ÉÇóºÍ²Ù×÷¡£

Ò»¡¢¸ÅÊö

ÔÚÊý¾Ý´¦ÀíÖÐ £¬ÇóºÍ²Ù×÷ÊǷǾ­³£¼ûµÄÐèÇó¡£ºÃ±È £¬ÎÒÃÇÐèҪͳ¼ÆÒ»×éÊý¾ÝÖеÄ×ÜºÍ £¬»òÕßijһÁеÄ×ÜºÍ £¬ÕâʱÎÒÃǾÍÐèҪʹÓõ½ÇóºÍ²Ù×÷¡£

ÔÚLaravelÖÐ £¬Ê¹ÓÃEloquent ORMÀ´»á¼ûÊý¾Ý¿â £¬¿ÉÒÔÖ±½ÓŲÓÃLaravelÌṩµÄsumº¯Êý¾ÙÐÐÇóºÍ²Ù×÷¡£

¶þ¡¢Ê¹ÓÃLaravelµÄsumÒªÁì

¼ÙÉèÎÒÃÇÓÐÒ»¸öorders±í £¬ÆäÖаüÀ¨ÒÔϼ¸¸ö×ֶΣº id, user_id, amount¡£

ÏÖÔÚ £¬ÎÒÃÇÐèÒª¶Ôamount×ֶξÙÐÐÇóºÍ²Ù×÷ £¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺

$totalAmount = DB::table('orders')->sum('amount');

µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃÇʹÓÃÁËLaravelµÄDBÃÅÃæÀ´»á¼ûorders±í £¬²¢Å²ÓÃÁËsumÒªÁì £¬´«ÈëÁË×Ö¶ÎÃûamount £¬×îºó»ñµÃÁË$totalAmount £¬ÌåÏָñíamount×ֶεÄ×ܺÍ¡£

³ýÁËÕû¸ö±íµÄÇóºÍ²Ù×÷ £¬ÎÒÃÇ»¹¿ÉÒԶԸñí¾ÙÐзÖ×éÇóºÍ¡£ºÃ±È £¬ÎÒÃÇÐèҪͳ¼Æÿ¸öÓû§(user_id)µÄ¶©µ¥½ð¶î×ÜºÍ £¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺

$sumByUser = DB::table('orders')->select('user_id', DB::raw('SUM(amount) as total'))->groupBy('user_id')->get();

µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃÇʹÓÃÁËselectÒªÁì £¬»ñÈ¡ÁËuser_idºÍamount×ֶΠ£¬²¢Ê¹ÓÃÁËDBµÄrawÒªÁì £¬½«SUMº¯Êý×÷ΪSQLÓï¾äµÄÒ»²¿·Ö £¬ÒÔ±ã»ñµÃ×ܺÍ×ܺÍ¡£Í¬Ê±Ò²Ê¹ÓÃÁËgroupByÒªÁì £¬¶Ôuser_id¾ÙÐÐÁË·Ö×é¡£

×îºó»ñµÃµÄЧ¹û¾Û»á»áÒéÊÇÒ»¸öÊý×é £¬Êý×éÖеÄÿһ¸öÔªËض¼°üÀ¨ÁËÒ»¸öÓû§µÄuser_id×ֶκ͸ÃÓû§¶©µ¥½ð¶î×ܺÍtotal×ֶΡ£

Èý¡¢¸öÐÔ»¯¶¨ÖÆ

ÔÚʹÓÃLaravelµÄsumÒªÁìµÄͬʱ £¬ÎÒÃÇ»¹¿ÉÒÔ¶ÔsqlÅÌÎÊÓï¾ä¾ÙÐиöÐÔ»¯¶¨ÖÆ £¬ÒÔ˳ӦԽ·¢ÖØ´óµÄÊý¾ÝÐèÇó¡£

ºÃ±È £¬ÎÒÃÇ¿ÉÒÔÔÚɸѡÌõ¼þÖÐÌí¼ÓһЩ×Ô½ç˵µÄÅÐ¶Ï £¬À´¹ýÂ˲»ÐèÒª¼ÆÈëÇóºÍµÄÊý¾Ý£º

$totalAmount = DB::table('orders')->where('status', 1)->sum('amount');

µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃÇÔÚÅÌÎÊ֮ǰÌí¼ÓÁËwhereÒªÁì £¬Ìí¼ÓÁËÒ»¸östatus×ֶΠ£¬ÌåÏÖÖ»ÅÌË㶩µ¥×´Ì¬Îª1µÄ¶©µ¥½ð¶î×ܺÍ¡£

ËÄ¡¢×ܽá

LaravelÌṩÁ˱ã½ÝµÄÇóºÍ²Ù×÷¹¦Ð§ £¬Àû±ãÁË¿ª·¢Õ߶ÔÊý¾Ý¾ÙÐÐͳ¼Æ¼°ÆÊÎö¡£Í¨¹ý±¾ÎĵÄÏÈÈÝ £¬¶ÁÕßÒѾ­Ñ§»áÔõÑùʹÓÃLaravelµÄsumÒªÁìÀ´Íê³ÉÇóºÍ²Ù×÷ £¬²¢ÇÒÏàʶµ½ÁËÔõÑù¶ÔsqlÅÌÎÊÓï¾ä¾ÙÐиöÐÔ»¯¶¨ÖÆ¡£ÕâЩ¹¦Ð§Îª¿ª·¢ÕßÌṩÁ˸ü´óµÄÎÞа¶È £¬Ê¹Êý¾Ý²Ù×÷±äµÃÔ½·¢¸ßЧºÍ±ã½Ý¡£

ÒÔÉϾÍÊÇlaravel sumÇóºÍµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ