【问题标题】:Get active and inactive records from the table从表中获取活动和非活动记录
【发布时间】:2019-03-30 19:12:39
【问题描述】:

我有两张桌子,例如:

tbl student :

.................
id  | Name |Age |
.................
1   | A    |15  |
2   | B    |13  |
3   | C    |12  |
.................

另一个表,例如 Tbl_student_details :

.......................
id  | stud_id |section |
.......................
1   | 1       |A       |
2   | 1       |B       |
3   | 2       |C       |
.......................


Result Expected: 
Active student : count (2) because id 1 and 2 have records in student detail table
Inactive Student :count(1)  dont have any record in student detail table

我需要得到的是我想要活跃或不活跃的学生..

如果任何学生在 student_details 中有记录,则需要这些学生的数量以及非活动学生的数量 哪个学生在学生详细信息表中没有记录我需要计算这些学生以及非活动学生的数量 我怎样才能让这个活跃和不活跃的学生有任何想法我需要在 laravel DB:: raw 中进行查询,如果不可能的话,只是建议我查询,请帮助我与此相关..

【问题讨论】:

  • 根据您的样本数据,预期的输出是什么?此外,从长远来看,存储 Age 没有任何意义,否则您将不得不每年自动递增它
  • 对非活动使用左连接,IS NULL Tbl_student_details.id 使用标准连接(INNER JOIN)用于其他连接。
  • 你在使用 laravel 关系吗?
  • 上尉。提莫@使用 DB:raw
  • 在您的模型中。你们有关系吗?

标签: php mysql sql laravel


【解决方案1】:

使用 laravel 你可以为你的表设置模型

class Student extends Model
{
    protected $table = 'student';

    public function details()
    {
        return $this->hasMany('App\StudentDetails');
    }
}


class StudentDetails extends Model
{
    protected $table = 'student_details';

    public function student()
    {
        return $this->belongsTo('App\Student');
    }
}

通过查询构建器,您可以获得计数为

$students = App\Student::withCount('details')->get();

以上将返回所有学生以及属性“details_count”,该属性将为您提供相关表中所需的计数

Counting Related Models

对于活跃/不活跃的学生,你可以写成

$activeStudents = App\Student::has('details')->count();
$inActiveStudents = App\Student::doesntHave('details')->count();

【讨论】:

    【解决方案2】:

    您可以通过将 student_details 表与 student 表左连接来使用 groupBy id 来获取每个 id 的计数。

     DB::table('student')
                    ->leftJoin('Tbl_student_details ', 'student.id', '=', 'Tbl_student_details .id')
                    ->groupBy('student.id')->count();
    

    【讨论】:

      【解决方案3】:
      • 为了考虑student 表中的所有学生,您需要使用Left Join,这样即使Tbl_student_details 表中没有匹配行,您仍然会考虑所有学生。
      • Count() 功能可用于统计非活跃/活跃学生。 Count() 函数不计算 NULL 值;所以我们可以使用Case .. When 表达式来计算不活跃的学生(NULL 因为不匹配的行)。
      • 为了计算活跃学生的唯一数量,我们需要使用Count(Distinct ..),因为Tbl_student_details 表中的stud_id 有重复的行。

      尝试以下查询:

      SELECT 
        COUNT(CASE WHEN tsd.id IS NULL THEN 1 END) AS Inactive_students, 
        COUNT(DISTINCT tsd.stud_id) AS Active_students 
      FROM 
      student AS ts 
      LEFT JOIN 
        Tbl_student_details AS tsd ON tsd.stud_id = ts.id 
      

      【讨论】:

        【解决方案4】:

        您可以尝试使用左连接和CASE WHEN

        select 
           s.id,
           s.`Name`,
           s.`Age`, case when sd.stud_id is null then 'In Active' else 'Active' end as `StudentStatus`
        from student s left join student_details sd 
        on s.id=sd.stud_id
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多