简体   繁体   English

如何在SQL中获取结果组

[英]How to get result group by in SQL

How to get_models_by_brand (grouped by body_type )? 如何get_models_by_brand (按body_type分组)?
I have those tables----> 1.models , 2.brands , 3.body_type 我有那些表----> 1.models2.brands3.body_type
I want to make query that get models by brand to be group by body_type like below 我想通过body_type下面的方式进行按品牌获取模型的查询

{
  {
  "sedan" : ["316","318"]
  },

  {
    "coupe" : ["z3","z4"]  
  },

}

This is how I get the brands ---- control file 这就是我获得品牌----控制文件的方式

public function getBrands()
{
    $brands = DB::table('brands')
              ->join('countries', 'brands.country_id', 'countries.country_id')
              ->where('brands_status', 1)
              ->select('brands.brands_id','brands.brands_name','brands.brands_name_ar',
              'brands.brands_logo_url','countries.country_name','countries.country_name_ar')
              ->get()->toArray();
    return response()->json(new Response(true, $brands));
}

它非常简单,您可以在查询中使用- > groupBy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM