1

大家好,我在回显我的数据时遇到了问题。我有三张桌子,一张用于学生、班级和学生班级。我已经查询了数据库以将这三个人联合起来,这样我就可以看到所有学生,无论他们是否已经参加了班级。sql是

SELECT students.id, students.student_name, students.student_number,
section_student.ss_id, section_student.section_number,sections.subject 
FROM students 
LEFT JOIN student_classes
ON students.student_number=student_classes.student_number 
LEFT JOIN classes 
ON classes.class_number=student_classes.class_number 
WHERE students.status=1

如果他们有课程,这会生成一个学生列表。对于那些注册了不止一门课程的学生,列表中会显示两次名称。

当我 print_r 我得到的数据

Array
(
[0] => stdClass Object
    (
        [class_number] => 
        [student_name] => john smith
        [student_number] => 432100351
        [id] => 2
        [ss_id] => 
        [subject] => 
        [user_email] => john@email.com
        [created_at] => 0000-00-00 00:00:00
        [updated_at] => 0000-00-00 00:00:00
    )

[1] => stdClass Object
    (
        [class_number] => 43223
        [student_name] => Paul jones
        [student_number] => 432100312
        [id] => 3
        [ss_id] => 40
        [subject] => Maths
        [user_email] => Paul@email.com
        [created_at] => 0000-00-00 00:00:00
        [updated_at] => 2012-11-24 08:33:23
    )

[2] => stdClass Object
    (
        [class_number] => 21331
        [student_name] => Paul jones
        [student_number] => 432100312
        [id] => 3
        [ss_id] => 39
        [subject] => Physics
        [user_email] => Paul@email.com
        [created_at] => 0000-00-00 00:00:00
        [updated_at] => 2012-11-24 08:33:23
    )

当我在表格中为每个人做一个以显示学生和他们的班级时,我想循环浏览学生在一行中的班级。例如

ID:432100351 | 不适用 Paul ID:432100312 | 43223 = 数学,21331 = 物理

当我做我的循环时,我得到

ID:432100351 约翰 | john@email.com | 不适用
ID:432100312 保罗 | 保罗@email.com | 43223 = 数学,21331 = 物理
ID:432100312 Paul | 保罗@email.com | 43223 = 数学,21331 = 物理

我不确定问题出在我的 sql 还是我的循环上

这是代码

@foreach ($students as $student)
<tr>
<td> {{ $key++ }}</td>
<td> {{ HTML::link('students/edit/' .$student->id, $student->student_number) }} </td>
<td> {{ HTML::link('students/edit/' .$student->id, $student->student_name) }} </td>
<td> {{ $student->user_email }} </td>
<td>


    @if(!isset($student->ss_id)) 

        {{ HTML::link('add_student_section.php?student_id='.$student->id,'Add to section'); }}

    @else 

        @foreach($students as $section) 

         {{ HTML::link('edit_student_section.php?ss_id=', $section->section_number . " " . $section->subject . " ") ;}}     

        @endforeach

    @endif    

</td>
<td> {{ date("d-M-Y H:i",strtotime($student->created_at )) }} </td>
<td> 
@if($student->updated_at == $student->created_at)

N/A

@else

{{ date("d-M-Y H:i",strtotime($student->updated_at )) }} 
@endif

</td>
<td> {{ HTML::link('students/delete/' .$student->id, 'Delete Student') }} </td>
</tr>
@endforeach

任何帮助,将不胜感激。谢谢

4

2 回答 2

0

在您的查询中使用distinctgroup by,如下例所示

SELECT students.id, students.student_name, students.student_number,
section_student.ss_id, section_student.section_number,sections.subject 
FROM students 
LEFT JOIN student_classes
ON students.student_number=student_classes.student_number 
LEFT JOIN classes 
ON classes.class_number=student_classes.class_number 
WHERE students.status=1 group by students.student_number,sections.subject
于 2012-11-25T09:34:20.463 回答
0

您需要将 GROUP BY 与GROUP_CONCAT结合使用

SELECT students.id, students.student_name, students.student_number,GROUP_CONCAT(classes.class_number) as student_classes
FROM students 
LEFT JOIN student_classes
ON students.student_number=student_classes.student_number 
LEFT JOIN classes 
ON classes.class_number=student_classes.class_number 
WHERE students.status=1 GROUP BY students.id
于 2012-11-25T09:48:06.703 回答