【问题标题】:left join not working in laravelleft join 在 laravel 中不起作用
【发布时间】:2015-10-22 19:12:41
【问题描述】:

student_attendance table structure student 我不知道为什么我无法使用 laravel 4 在 mysql 中正确使用左连接

我的“学生”表结构是:

id|name

'attendance_student' 表是:

id|date|student_id

我想显示所有学生以及他们在某一天的出席或缺席情况。

例如在某一天,如果学生缺席,其 id 和日期将输入到 'attendance_student' 表中

我的查询:

$student = DB::table('students')
        ->leftJoin('attendance_student', 'students.id', '=', 'attendance_student.student_id')
        ->select('students.id', 'students.name', 'attendance_student.present')
        ->where('attendance_student.date', '=', '2015-10-14')
        ->get();

echo '<table>';
            foreach ( $student as $attendance ) 
            {
                echo "<tr>";
                echo "<td>$attendance->name</td>";
                echo "<td>$attendance->present</td></tr>";
            }
echo '</table>';

但是,如果某天学生缺席,我只能获取该学生的数据。我的问题是我想显示该班级的所有学生。目前我只能通过这个显示缺勤学生

【问题讨论】:

  • 哪个字段和值表示学生在场/缺席?
  • 1 表示存在,0 表示不存在

标签: php mysql laravel-4


【解决方案1】:

我遇到了类似的问题,我找到的解决方案是下一个

替换这个

->leftJoin('attendance_student', 'students.id', '=', 'attendance_student.student_id')

为此

->leftJoin('attendance_student', function($join){
            $join->on('students.id', '=', 'attendance_student.student_id');
            $join->on('attendance_student.date', '=', DB::raw('2015-10-14'));
        })

最后删除你的

->where('attendance_student.date', '=', '2015-10-14')

因为当您在哪里进行时,您会强制查询在

中进行注册

出勤学生

祝你好运!

【讨论】:

    【解决方案2】:

    您想要左连接,但您只是使用常规连接。你应该使用-&gt;leftJoin

    【讨论】:

    • 这是在 SO 中发布问题时的拼写错误。已进行编辑,但结果相同
    【解决方案3】:

    问题是你的WHERE,改成:

    ->where('attendance_student.date', '=', '2015-10-14')
    ->orWhere('attendance_student', '=', null)
    

    当你使用 leftJoin 时,它不需要匹配。但是在你的 WHERE 中,它需要设置日期(日期 = 2015-10-14),这与将 leftJoin 替换为 join 相同。

    【讨论】:

    • 更改了查询但仍然无法正常工作 $student = DB::table('students') ->leftJoin('attendance_student', 'students.id', '=', 'attendance_student.student_id') ->select('students.id', 'students.name', 'attendance_student.present') ->where('attendance_student.date', '=', '2015-10-14') ->orWhere('attendance_student .date', '=', NULL) ->get();
    • 如果您尝试删除where 会怎样?除了它会显示所有日期之外,结果是你想要的吗?
    • 会得到表的所有数据。理想情况下希望所有学生及其缺勤状态
    • 那么,在第一个版本中,您只能获取attendance_student 中不存在的行?在这种情况下,您在数据库中的列看起来如何?日期、时间戳、日期时间...?
    • 它是一个日期字段。已添加表格数据图片
    【解决方案4】:

    所以显然你只有缺勤学生的约会......(?)。

    如果是这样......我会尝试一些看起来像:

    SELECT students.id, students.name, 0 as absent FROM students 
    WHERE students.id not in (
         SELECT attendance_student.id FROM attendance_student 
         WHERE attendance_student.date = '2015-10-14'
    )
    
    UNION
    
    SELECT students.id, students.name, attendance_student.present as absent 
    FROM students INNER JOIN attendance_student 
    ON students.id = attendance_student.student_id 
    WHERE attendance_student.date = '2015-10-14'
    

    因此,首先让所有在该特定日期出现的学生,然后将他们与缺席的学生联合起来。

    附:我正在“重命名” admission_student.present 为“缺席”,只是为了在我们阅读它时更有意义,但这并不重要。

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 2018-08-18
      • 2015-12-05
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2023-03-07
      • 2018-11-30
      相关资源
      最近更新 更多