在没有在每个循环中循环的情况下对SQL搜索进行分组 - 有更好的方法吗?

时间:2018-02-23 12:22:59

标签: php sql

我有一个学生数据库,我想根据他们的表格类别对每个学生进行分组。目前我只能通过在每个组内循环来完成此操作。有一个更好的方法吗。我希望结果看起来像这样:

  • 第1年
    • 表格1
      • Pupil Y,Pupil,Pupil K
    • 表格2
      • Pupil G,Pupil A,Pupil Z
  • 第2年
    • 表格3
      • Pupil A,Pupil U,Pupil Q
    • 表格4
      • Pupil V,Pupil R,Pupil M
  • 3年级
    • 表格第5类
      • Pupil G,Pupil S,Pupil D
    • 表格6
      • Pupil W,Pupil P,Pupil C

数据库如下所示:

+----+------+----------+-------+
|Year|Class |First Name|Surname|
+----+------+----------+-------+
|1   |Class3|Jenny     |James  | 
|1   |Class1|John      |Bruces |  
|1   |Class1|Dirk      |Diggler|
|1   |Class2|Michael   |Johnson|
+----+------+----------+-------+

我到目前为止尝试的编码是:

<?php
$pupil_details = $_SESSION['pupdetails'];
$selectclass   = $connect->query("SELECT DISTINCT Year FROM `$pupil_details` ORDER BY class DESC");

while ($rows = $selectclass->fetch_array()) {                
    echo $rows["Year"];
    $yrlvl = $rows['Year'];
?><br/><?php              
    $selectpup = $connect->query("SELECT DISTINCT class from `$pupil_details` where Year ='$yrlvl'");

    while ($rowsd = $selectpup->fetch_array()) {
        $yrlvlclass = $rowsd['class'];
        echo $yrlvlclass;
?><br/><?php        
        $wholeSchool = mysqli_query($connect, "SELECT * from $pupil_details where class ='$yrlvlclass' ORDER BY surname asc");

        while ($row = mysqli_fetch_array($wholeSchool)) {
            echo $row["firstname"];
            echo $row["surname"];                                          
        }
    }
}
?>

有人能建议更好的方法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用单个查询完成所有操作,只需按年份,班级和姓氏进行排序

 select  * 
 from  your_table 
 order by Year, class, surname 

但是不要在你的php代码中使用var ..你有进行SQL注入的风险..在你的情况下,在SQL中使用对象(表名)不允许在SQL中

答案 1 :(得分:1)

我对此进行了测试并且有效:

$selectPupil   = $connect->query("select  *  from  `pupils` order by Year, Class, Surname");

$neededForFirstValue = 0;

while ($row = $selectPupil->fetch_array()) { 
    //set initial values for year
    if($neededForFirstValue == 0) {
        $previousYear = $row['Year']; //takes the value to compare
        echo "Year " . $previousYear . "<br />"; //display first year
        $neededForFirstValue = 1; //to never go into that if again
    }
    $currentYear = $row['Year']; //takes current year value

    if($currentYear == $previousYear) { //check if year has changed, if not, keep going
        //set initial values for class
        if($neededForFirstValue == 1) {
            $previousClass = $row['Class']; //takes the value to compare
            echo "Class: " . $previousClass . "<br />"; //display first year
            $neededForFirstValue = 2; //to never go into any if again
        }
        $currentClass = $row['Class'];
        if($currentClass == $previousClass) {
            echo $row['First Name'] . " " . $row['Surname'] . "<br />"; //display a pupil
        } else {
            echo "Class: " . $currentClass . "<br />"; //display current year
            echo $row['First Name'] . " " . $row['Surname'] . "<br />"; //display first user in each class
        }
    } else {
        echo "Year " . $currentYear . "<br />"; //display current year
    }
    $previousClass = $row['Class']; //remember the year to compare
    $previousYear = $row['Year']; //remember the year to compare
}