尊龙凯时人生就是搏

laravel group byÓ÷¨

laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ÆÕ±éÓ¦ÓÃÓÚweb¿ª·¢¡£ÆäÖУ¬laravelÖеÄgroup byÓ÷¨ÊǷǾ­³£ÓõÄ£¬¿ÉÒÔÔÚÊý¾Ý¿âÖжÔÊý¾Ý¾ÙÐзÖ×飬Àû±ã¾ÙÐÐͳ¼ÆÆÊÎöºÍ¾ÛºÏ²Ù×÷¡£±¾ÎĽ«ÉîÈë̽ÌÖlaravelÖеÄgroup byÓ÷¨£¬×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÓ¦Óá£

Ò»¡¢Ê²Ã´ÊÇgroup by

ÔÚÊý¾Ý¿âÖУ¬group byÊÇÒ»ÖÖ¶ÔÊý¾Ý¾ÙÐзÖ×éµÄ²Ù×÷¡£Ëü½«¾ßÓÐÏàͬÊôÐÔÖµµÄÊý¾ÝÐйéΪͳһÖֱ𣬲¢ÔÚ´Ë»ù´¡ÉϾÙÐÐͳ¼ÆºÍ¾ÛºÏ²Ù×÷¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃgroup byÒªÁìÀ´ÊµÏִ˹¦Ð§¡£

ÔÚLaravelÖУ¬group byÒªÁìµÄÃûÌÃÈçÏ£º

$users = DB::table('users')
               ->groupBy('account_id')
               ->having('account_id', '>', 100)
               ->get();

µÇ¼ºó¸´ÖÆ

Õâ¸öÒªÁì½ÓÊÜÒ»¸ö²ÎÊý£¬¼´Òª·Ö×éµÄ×Ö¶ÎÃû¡£ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃǽ«Óû§±íƾ֤account_idÕâÒ»×ֶξÙÐзÖ×飬²¢É¸Ñ¡³öaccount_id´óÓÚ100µÄ¼Í¼¡£×îÖÕ£¬ÎÒÃÇ»ñµÃÁËÒ»¸öÓû§Áбí£¬ÆäÖÐÿ¸öÓû§¶¼±»·Ö×éµ½ÁËËüÃÇËùÊôµÄÕË»§ÖС£

¶þ¡¢ÔÚLaravelÖÐʹÓÃgroup byµÄ³¡¾°

ÔÚÏÖʵ¿ª·¢ÖУ¬LaravelÖеÄgroup byÓ÷¨ºÜÊÇÎÞа£¬¿ÉÒÔÓ¦ÓÃÓÚÖÖÖÖ³¡¾°¡£ÏÂÃæÊÇһЩ³£¼ûµÄʹÓó¡¾°£º

ͳ¼ÆÆÊÎö

LaravelÖеÄgroup byÓ÷¨¿ÉÒÔ×ÊÖúÎÒÃǾÙÐÐÖÖÖÖͳ¼ÆÆÊÎö£¬ºÃ±ÈÅÌËãƽ¾ùÖµ¡¢×î´óÖµ¡¢×îСֵ¡¢ÇóºÍµÈµÈ¡£ÔÚÕâÖÖÇéÐÎÏ£¬ÎÒÃÇͨ³£ÐèÒªÏȽ«Êý¾Ýƾ֤ij¸ö×ֶξÙÐзÖ×飬Ȼºó¶Ô¸÷¸ö×éÄÚµÄÊý¾Ý¾ÙÐоۺϲÙ×÷¡£

ºÃ±È£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂëÀ´ÅÌËãÿ¸öÄê·ÝµÄÏúÊÛ×ܶ

$sales = DB::table('orders')
            ->select(DB::raw('YEAR(created_at) as year'), DB::raw('SUM(total) as sales'))
            ->groupBy('year')
            ->get();

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«¶©µ¥±íƾ֤¶©µ¥½¨Éèʱ¼äµÄÄê·Ý¾ÙÐзÖ×飬²¢Ê¹ÓÃDB::rawÒªÁìÀ´¾ÙÐоۺϲÙ×÷¡£

Êý¾ÝÈ¥ÖØ

ÓÐʱ¼äÎÒÃÇÐèÒª¶ÔÊý¾Ý¾ÙÐÐÈ¥ÖØ£¬ÒÔ×èÖ¹Öظ´Í³¼ÆºÍÅÌËã¡£LaravelÖеÄgroup byÓ÷¨¿ÉÒÔºÜÀû±ãµØʵÏÖÈ¥ÖزÙ×÷¡£

ºÃ±È£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊûÓÐÖظ´µÄÓû§ÁÐ±í£º

$users = DB::table('users')
             ->groupBy('email')
             ->get();

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«Óû§±íƾ֤email×ֶξÙÐзÖ×飬ÒÔÈ·±£Ã¿¸öÓÊÏäµØµãÖ»ÓÐÒ»¸öÓû§¡£

¶à±í¹ØÁª

µ±ÎÒÃÇÐèÒª¶Ô¶àÕűí¾ÙÐйØÁªÅÌÎÊʱ£¬group byÓ÷¨¿ÉÒÔ×ÊÖúÎÒÃǶÔЧ¹û¾ÙÐзÖ×飬ÒÔ±ã¾ÙÐкóÐøµÄͳ¼ÆºÍɸѡ²Ù×÷¡£

ºÃ±È£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÅÌÎÊÿ¸öÆ·ÀàµÄ¿â´æ×ÜÁ¿£º

$categoryStocks = DB::table('products')
                       ->join('categories', 'products.category_id', '=', 'categories.id')
                       ->select('categories.name', DB::raw('SUM(products.stock) as total'))
                       ->groupBy('categories.name')
                       ->get();

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«²úÆ·±íºÍÖÖ±ð±í¾ÙÐÐÁ˹ØÁª£¬²¢Æ¾Ö¤ÖÖÓÖÃû³Æ½«²úÆ·¿â´æ×ÜÁ¿¾ÙÐÐÁË·Ö×é¡£×îÖÕ£¬ÎÒÃÇ»ñµÃÁËÒ»¸öÖÖ±ð¿â´æ×ÜÁ¿µÄÁбí¡£

Èý¡¢Ê¹ÓÃ×¢ÖØÊÂÏî

ËäÈ»LaravelÖеÄgroup byÓ÷¨ºÜÀû±ã£¬µ«ÎÒÃÇÔÚʹÓõÄʱ¼äÕÕ¾ÉÐèҪעÖØһЩÊÂÏÒÔÈ·±£³ÌÐòµÄ׼ȷÐÔºÍÐÔÄÜ¡£

ÔÚʹÓÃgroup byÒªÁì֮ǰ£¬ÎÒÃÇÓ¦¸Ã¾¡¿ÉÄܵØʹÓÃwhere»òhavingÒªÁì¾ÙÐÐɸѡ²Ù×÷£¬ÒÔïÔÌ­ÐèÒª·Ö×éµÄÊý¾ÝÁ¿¡£

ºÃ±È£¬ÈôÊÇÎÒÃÇÐèÒªÅÌÎÊÏúÊÛ¶î´óÓÚ10000µÄÄê·ÝÁбí£¬ÄÇôÎÒÃÇÓ¦¸ÃÏÈʹÓÃwhereÒªÁìɸѡµôСÓÚ10000µÄÊý¾Ý£¬È»ºóÔÙ¾ÙÐÐgroup by²Ù×÷£º

$sales = DB::table('orders')
            ->select(DB::raw('YEAR(created_at) as year'), DB::raw('SUM(total) as sales'))
            ->where('total', '>', 10000)
            ->groupBy('year')
            ->get();

µÇ¼ºó¸´ÖÆ

ÕâÑùÒ»À´£¬ÎÒÃÇ¿ÉÒÔïÔÌ­´ý·Ö×éÊý¾ÝµÄÊýÄ¿£¬Ìá¸ßÅÌÎÊЧÂÊ¡£

ÔÚʹÓÃgroup byÒªÁìʱ£¬ÎÒÃÇÓ¦¸Ã¾¡¿ÉÄܵØʹÓþۺϺ¯Êý£¬ÒÔ×èÖ¹·ºÆðÊý¾Ý¹ýʧ¡£

ºÃ±È£¬ÈôÊÇÎÒÃÇÐèÒªÅÌÎÊÏúÊÛ¶î´óÓÚƽ¾ùÖµµÄÄê·ÝÁбí£¬ÄÇôÎÒÃÇÐèҪʹÓÃAVGº¯ÊýÀ´ÅÌËãƽ¾ùÏúÊ۶

$sales = DB::table('orders')
            ->select(DB::raw('YEAR(created_at) as year'), DB::raw('AVG(total) as average'))
            ->groupBy('year')
            ->having('average', '>', 100)
            ->get();

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËAVGº¯ÊýÀ´ÅÌËãƽ¾ùÏúÊ۶ÈôÊÇÎÒÃÇÖ±½ÓʹÓÃSUMº¯Êý½«ÏúÊÛ¶î¾ÙÐÐÀÛ¼Ó£¬ÄÇôÅÌËãЧ¹û¾ÍÊǹýʧµÄ¡£

ÔÚʹÓÃgroup byÒªÁìʱ£¬ÎÒÃÇÓ¦¸Ã×¢ÖØÐÔÄÜÎÊÌ⣬×èÖ¹¶Ô´ó×ÚÊý¾Ý¾ÙÐзÖ×é¡£

ÔÚijЩÇéÐÎÏ£¬ÎÒÃÇÐèÒª¶Ô´ó×ÚÊý¾Ý¾ÙÐÐgroup by²Ù×÷£¬Õâ¿ÉÄܻᵼÖ³ÌÐòÐÔÄÜϽµ¡£ÎªÁË×èÖ¹Õâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔ˼Á¿Ê¹ÓÃË÷Òý»ò·ÖÇø±íÀ´ÓÅ»¯ÅÌÎÊÐÔÄÜ¡£

ºÃ±È£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´½¨ÉèÒ»¸ö¶Ô¶©µ¥±íµÄʱ¼ä×ֶν¨ÉèË÷Òý£º

ALTER TABLE orders ADD INDEX (created_at);

µÇ¼ºó¸´ÖÆ

ÕâÑùÒ»À´£¬ÎÒÃÇ¿ÉÒÔÔÚ¾ÙÐÐgroup by²Ù×÷ʱ¼ÓËÙÅÌÎÊËÙÂÊ£¬Ìá¸ß³ÌÐòÐÔÄÜ¡£

ËÄ¡¢×ܽá

LaravelÖеÄgroup byÓ÷¨ÊǺÜÊÇÎÞаºÍÀû±ãµÄ£¬¿ÉÒÔ×ÊÖúÎÒÃǶÔÊý¾Ý¿â¾ÙÐÐÖÖÖÖͳ¼ÆÆÊÎöºÍ¾ÛºÏ²Ù×÷¡£µ«ÎÒÃÇÔÚʹÓÃËüʱ»¹ÐèҪעÖØһЩÊÂÏÒÔÈ·±£³ÌÐòµÄ׼ȷÐÔºÍÐÔÄÜ¡£Ï£Íû±¾ÎÄÄܹ»×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÓ¦ÓÃLaravelÖеÄgroup byÓ÷¨£¬´Ó¶øÌá¸ß¿ª·¢Ð§ÂʺͳÌÐòÖÊÁ¿¡£

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

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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