Php 连接3个表并计算一列的平均值

Php 连接3个表并计算一列的平均值,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我目前正在学习Laravel,在加入三个表时使用DB类。我可以加入这三个表,但我需要得到某一列老师的平均值(评分表,评分列),这是我所拥有的,我被困在这里了 这是我的桌子设计 这是我的问题 $teachers = DB::table('ratings as r') ->join('users as u','r.teacher_id', '=', 'u.id') ->join('user_infos as

我目前正在学习Laravel,在加入三个表时使用DB类。我可以加入这三个表,但我需要得到某一列老师的平均值(评分表,评分列),这是我所拥有的,我被困在这里了

这是我的桌子设计

这是我的问题

$teachers = DB::table('ratings as r')
                    ->join('users as u','r.teacher_id', '=', 'u.id')
                    ->join('user_infos as ui','r.teacher_id', '=', 'ui.user_id')
                    ->select('r.rating','ui.about','ui.first_name','ui.last_name','ui.avatar','ui.rate','u.*')
                    ->where('u.status', 1)->get();
同样,相同用户的结果也在重复。用户在ratings表中有两个评级,它在我的视图中出现两次


我想在这里展示的是所有教师的名单,每张卡片上都有相应的评分。。因此,如果表中有两位教师,它将显示这两位教师,卡片右上方是他们的评分。

这里有一个可能的解决方案:

$teachers = DB::table('ratings as r')
                ->join('users as u','r.teacher_id', '=', 'u.id')
                ->join('user_infos as ui','r.teacher_id', '=', 'ui.user_id')
                ->select(DB::raw('AVG(r.rating) as average_rating'),'ui.about','ui.first_name','ui.last_name','ui.avatar','ui.rate','u.*')
                ->groupBy('r.teacher_id')
                ->where('u.status', 1)->get();

以下是一个可能的解决方案:

$teachers = DB::table('ratings as r')
                ->join('users as u','r.teacher_id', '=', 'u.id')
                ->join('user_infos as ui','r.teacher_id', '=', 'ui.user_id')
                ->select(DB::raw('AVG(r.rating) as average_rating'),'ui.about','ui.first_name','ui.last_name','ui.avatar','ui.rate','u.*')
                ->groupBy('r.teacher_id')
                ->where('u.status', 1)->get();

好的..因为您使用的是Laravel命名约定/推荐,所以我认为如果您使用雄辩的话会更容易/更干净

我不确定你是否已经创造了雄辩的模型。正因为如此,我将把所有的东西都放在这里(模型等)

评级模型

class Rating extends Model 
{
  protected $guard = ['id'];

  public function teacher()
  {
    return $this->belongsTo(User::class, 'teacher_id');
  }

  public function student()
  {
    return $this->belongsTo(User::class, 'student_id');
  }
}
class UserInfo extends Model 
{
  protected $guard = ['id'];

  public function user()
  {
    return $this->belongsTo(User::class);
  }
}
class User extends Model 
{
  protected $guard = ['id'];

  public function ratings() 
  {
    return $this->hasMany(Rating::class, 'teacher_id');
  }

  public function infos() 
  {
    return $this->hasMany(UserInfo::class);
  }
}
用户信息模型

class Rating extends Model 
{
  protected $guard = ['id'];

  public function teacher()
  {
    return $this->belongsTo(User::class, 'teacher_id');
  }

  public function student()
  {
    return $this->belongsTo(User::class, 'student_id');
  }
}
class UserInfo extends Model 
{
  protected $guard = ['id'];

  public function user()
  {
    return $this->belongsTo(User::class);
  }
}
class User extends Model 
{
  protected $guard = ['id'];

  public function ratings() 
  {
    return $this->hasMany(Rating::class, 'teacher_id');
  }

  public function infos() 
  {
    return $this->hasMany(UserInfo::class);
  }
}
用户模型

class Rating extends Model 
{
  protected $guard = ['id'];

  public function teacher()
  {
    return $this->belongsTo(User::class, 'teacher_id');
  }

  public function student()
  {
    return $this->belongsTo(User::class, 'student_id');
  }
}
class UserInfo extends Model 
{
  protected $guard = ['id'];

  public function user()
  {
    return $this->belongsTo(User::class);
  }
}
class User extends Model 
{
  protected $guard = ['id'];

  public function ratings() 
  {
    return $this->hasMany(Rating::class, 'teacher_id');
  }

  public function infos() 
  {
    return $this->hasMany(UserInfo::class);
  }
}
您的问题可能的查询解决方案:

$ratings = Rating::with(['teacher.infos', 'student.infos'])->whereHas('teacher', function($q) {
  $q->where('status', true);
})->get();
这可能会给你类似的东西:

// ratings: [
//  "id": 1,
//  "teacher_id": 1,
//  "student_id": 2,
//  ....
//  "teacher": {
//    "id": 1,
//    "name": "...."
//    ...
//    "infos": [{
//      "id": 1,
//      "skype": '....'
//      ....
//    }]
//  },
//  "student": {
//    "id": 2,
//    "name": ....,
//    "infos": [{
//      "id": ...
//    }]
//  }
// ]
现在你有了一个收视率的集合。如果你需要访问用户或用户信息,你只需要

// Example:
$firstStudentInfo = $ratings->isEmpty() ? null : $ratings->first()->student->infos;
如果您需要计算一些东西,您可以使用额外的查询(db),也可以只使用一个方法 收集我认为,在这种情况下,收集可以更快。还可以创建特定的集合 对于您的“评级”模型,使用特定计算(“评级集合”)

另一个示例(刀片模板)。既然我们已经加载了所有“急切加载”,我们就不必担心了 这里有N+1个查询问题。()


如果你还想使用DB,@Md Mahfuzur Rahman会帮你的


:)

好的..由于您使用的是Laravel命名约定/推荐,我认为如果您使用雄辩的,会更容易/更干净

我不确定你是否已经创造了雄辩的模型。正因为如此,我将把所有的东西都放在这里(模型等)

评级模型

class Rating extends Model 
{
  protected $guard = ['id'];

  public function teacher()
  {
    return $this->belongsTo(User::class, 'teacher_id');
  }

  public function student()
  {
    return $this->belongsTo(User::class, 'student_id');
  }
}
class UserInfo extends Model 
{
  protected $guard = ['id'];

  public function user()
  {
    return $this->belongsTo(User::class);
  }
}
class User extends Model 
{
  protected $guard = ['id'];

  public function ratings() 
  {
    return $this->hasMany(Rating::class, 'teacher_id');
  }

  public function infos() 
  {
    return $this->hasMany(UserInfo::class);
  }
}
用户信息模型

class Rating extends Model 
{
  protected $guard = ['id'];

  public function teacher()
  {
    return $this->belongsTo(User::class, 'teacher_id');
  }

  public function student()
  {
    return $this->belongsTo(User::class, 'student_id');
  }
}
class UserInfo extends Model 
{
  protected $guard = ['id'];

  public function user()
  {
    return $this->belongsTo(User::class);
  }
}
class User extends Model 
{
  protected $guard = ['id'];

  public function ratings() 
  {
    return $this->hasMany(Rating::class, 'teacher_id');
  }

  public function infos() 
  {
    return $this->hasMany(UserInfo::class);
  }
}
用户模型

class Rating extends Model 
{
  protected $guard = ['id'];

  public function teacher()
  {
    return $this->belongsTo(User::class, 'teacher_id');
  }

  public function student()
  {
    return $this->belongsTo(User::class, 'student_id');
  }
}
class UserInfo extends Model 
{
  protected $guard = ['id'];

  public function user()
  {
    return $this->belongsTo(User::class);
  }
}
class User extends Model 
{
  protected $guard = ['id'];

  public function ratings() 
  {
    return $this->hasMany(Rating::class, 'teacher_id');
  }

  public function infos() 
  {
    return $this->hasMany(UserInfo::class);
  }
}
您的问题可能的查询解决方案:

$ratings = Rating::with(['teacher.infos', 'student.infos'])->whereHas('teacher', function($q) {
  $q->where('status', true);
})->get();
这可能会给你类似的东西:

// ratings: [
//  "id": 1,
//  "teacher_id": 1,
//  "student_id": 2,
//  ....
//  "teacher": {
//    "id": 1,
//    "name": "...."
//    ...
//    "infos": [{
//      "id": 1,
//      "skype": '....'
//      ....
//    }]
//  },
//  "student": {
//    "id": 2,
//    "name": ....,
//    "infos": [{
//      "id": ...
//    }]
//  }
// ]
现在你有了一个收视率的集合。如果你需要访问用户或用户信息,你只需要

// Example:
$firstStudentInfo = $ratings->isEmpty() ? null : $ratings->first()->student->infos;
如果您需要计算一些东西,您可以使用额外的查询(db),也可以只使用一个方法 收集我认为,在这种情况下,收集可以更快。还可以创建特定的集合 对于您的“评级”模型,使用特定计算(“评级集合”)

另一个示例(刀片模板)。既然我们已经加载了所有“急切加载”,我们就不必担心了 这里有N+1个查询问题。()


如果你还想使用DB,@Md Mahfuzur Rahman会帮你的


:)

您应该首先尝试为您的问题编写原始MySQL查询。如果你甚至不能做到这一点,那就忘了Laravel代码吧。话虽如此,你能告诉我们原始的查询吗?或者,如果没有,您能告诉我们预期的输出应该是什么吗?我发现发生了什么,
ratings
表是空的,因此不会返回结果,因为第二次连接没有匹配的记录
->join('user\u infos as ui','r.teacher\u id','=','ui.user\u id')
。。当我尝试手动将一条记录放入
ratings
表时,返回了一个结果..您应该首先尝试为您的问题编写原始MySQL查询。如果你甚至不能做到这一点,那就忘了Laravel代码吧。话虽如此,你能告诉我们原始的查询吗?或者,如果没有,您能告诉我们预期的输出应该是什么吗?我发现发生了什么,
ratings
表是空的,因此不会返回结果,因为第二次连接没有匹配的记录
->join('user\u infos as ui','r.teacher\u id','=','ui.user\u id')
。。当我尝试手动将记录放入
评级
表时,返回了一个结果。。