嵌套为每个循环或SQL复制结果

时间:2012-11-25 09:28:54

标签: php mysql foreach laravel

我所有的问题都在回应我的数据。 我有三个表,一个用于学生,班级和student_classes。 我已经查询了数据库以联合所有这三个,所以我可以看到所有学生,无论他们是否已经参加了课堂上课。 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

如果他们有课程,则会生成一个包含课程的学生列表。对于那些已经注册了多门课程的学生,名单会在名单中显示两次。

当我打印__数据时

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 | N / A 保罗ID:432100312 | 43223 =数学,21331 =物理

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

ID:432100351 john | john@email.com | N / A
ID:432100312保罗| paul@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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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