簡體   English   中英

While循環顯示在不同的行而不是同一行

[英]While loop displaying over different rows instead of same row

我有三個表:

Student - UPN, Name, Year, House
Seclusion_Status - ID, Arrived, FTE, Rebuild, DateTimeAdded, Staff, Student_UPN (fk), Comment
Period_Rating - ID, Slot_ID, Rating, Date, Seclusion_ID (fk)

每個學生在Seclusion_Status表中可以有很多條目,然后在Period_rating表中也有很多條目,該表通過Seclusion_ID鏈接到Seclusion_status表。

我正在運行以下查詢,以基於日期從Seclusion_Status返回一條記錄,然后返回Period_rating表中與Seclusion_status記錄相關的所有記錄。

$sql="SELECT * FROM Seclusion_Status 
  INNER JOIN Students ON Seclusion_Status.Student_UPN=Students.UPN 
  JOIN Period_Rating ON Seclusion_Status.ID=period_rating.Seclusion_ID
  WHERE period_rating.Date = '$start' 
  ORDER BY Seclusion_Status.DateTimeAdded ASC";
$result=mysql_query($sql);

然后,我使用while循環遍歷結果:

while($rows=mysql_fetch_array($result)){

然后在表中顯示結果。 我遇到的問題是,在period_rating表中有多個條目的地方,每個學生都在不同的行而不是同一行上重復。

這是我用來從while循環中寫出數據的代碼:

<tbody>
<tr>            
    <!--Write out the Student name and year group, and set the colour based on their House/College-->
                      <?php if($rows['House'] == 'Acer') {
                      echo '<td width="150px" bgcolor="#003399">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' .  '</td>';
                      }else if($rows['House'] == 'Clarus') {    
                      echo '<td width="150px" bgcolor="#FF0000">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' .  '</td>';   
                      }else if($rows['House'] == 'Fortis') {    
                      echo '<td width="150px" bgcolor="#02A10C">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' .  '</td>';   
                      }else{
                      echo '<td width="150px" bgcolor="#D2D904">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' .  '</td>';       
                      }
                      ?>

        <!--Write Out the Staff Name-->
        <td><p><?php echo $rows['Staff']; ?> </p></td>

        <!--Write out the comment and the incident type in brackets-->
        <td width="210px"><p><?php echo $rows['Comment']; ?> (<?php echo $rows['Incident']; ?>) </p></td>

        <!--Start writing out the ratings for Period 1-->
            <form action="P1Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>&startdate=<?php echo $start; ?>" method="post">
            <?php 
                if ($rows['Slot_ID'] == P1)
                {
                    if (empty($rows['Rating'])) 
                    {
                        echo '<td><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P1" id="P1" maxlength="15"   size="1"><option disabled selected></option><option>G</option><option>A</option><option>R</option></td>';
                    }else if ($rows['Rating'] == G)
                    {
                        echo '<td bgcolor="#02A10C">' . $rows['Rating'] . '</td>';
                    }else if ($rows['Rating'] == A)
                    {
                        echo '<td bgcolor="#ff9900">' . $rows['Rating'] . '</td>';
                    }else if ($rows['Rating'] == R)
                    {
                        echo '<td bgcolor="#FF0000">' . $rows['Rating'] . '</td>';
                    }
                }else
                {
                    echo '<td><img src="images\add.png" width="20px"></td>';
                }
            ?>
            </form>

            <!--Start writing out the ratings for Period 2-->
            <form action="P2Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>&startdate=<?php echo $start; ?>" method="post">
            <?php 
                if ($rows['Slot_ID'] == P2)
                {
                    if (empty($rows['Rating'])) 
                    {
                        echo '<td><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P2" id="P2" maxlength="15"   size="1"><option disabled selected></option><option>G</option><option>A</option><option>R</option></td>';
                    }else if ($rows['Rating'] == G)
                    {
                        echo '<td bgcolor="#02A10C">' . $rows['Rating'] . '</td>';
                    }else if ($rows['Rating'] == A)
                    {
                        echo '<td bgcolor="#ff9900">' . $rows['Rating'] . '</td>';
                    }else if ($rows['Rating'] == R)
                    {
                        echo '<td bgcolor="#FF0000">' . $rows['Rating'] . '</td>';
                    }
                    }else
                    {
                    echo '<td><img src="images\add.png" width="20px"></td>';
                }
            ?>
            </form>

這是數據的一個例子

Students:

UPN | name | House | Year
---------------------------
112  | john  | Acer  | Year 9
113  | jack  | Acer  | Year 9

Seclusion_Status:

id | Student_UPN | Arrived | FTE | etc
-----------------------------
1  | 112          | Y      | N   | 
2  | 113          | N      | N   |

Period_rating:

id | Slot_ID | Rating | Seclusion_ID |
----------------------------------------
1  | P1      | G      | 1
2  | P2      | R      | 1
3  | P3      | G      | 1

當前返回的是:

 Name | Student_UPN | Slot_ID | Rating
---------------------------------------------
John  | 112         | P1      | G
John  | 112         | P2      | R
John  | 112         | P3      | G

我想要的是:

Name | Student_UPN | P1 Rating | P2 Rating | P3 Rating
---------------------------------------------
John  | 112         | G        | R         | R

希望這更有意義嗎?

因此,按照鏈接的問題,我已將查詢更新為該查詢,但仍無法正常工作?

 $sql="SELECT *
                MAX(case when period_rating.Slot_ID = 'P1' THEN Rating ELSE Null END) 'P1',
                MAX(case when period_rating.Slot_ID = 'P2' THEN Rating ELSE Null END) 'P2',
                MAX(case when period_rating.Slot_ID = 'LF' THEN Rating ELSE Null END) 'LF',
                MAX(case when period_rating.Slot_ID = 'BR' THEN Rating ELSE Null END) 'BR',
                MAX(case when period_rating.Slot_ID = 'P3' THEN Rating ELSE Null END) 'P3',
                MAX(case when period_rating.Slot_ID = 'P4' THEN Rating ELSE Null END) 'P4',
                MAX(case when period_rating.Slot_ID = 'LC' THEN Rating ELSE Null END) 'LC',
                MAX(case when period_rating.Slot_ID = 'P5' THEN Rating ELSE Null END) 'P5',
                MAX(case when period_rating.Slot_ID = 'P6' THEN Rating ELSE Null END) 'P6',
                MAX(case when period_rating.Slot_ID = 'DT' THEN Rating ELSE Null END) 'DT'
            FROM Seclusion_Status
            INNER JOIN Students
                ON Seclusion_Status.Student_UPN=Students.UPN
            INNER JOIN Period_Rating
                ON Seclusion_Status.ID=period_rating.Seclusion_ID
            WHERE period_rating.Date = '$start'
            GROUP BY Seclusion_Status.Student_UPN
            ORDER BY Seclusion_Status.DateTimeAdded ASC";

您似乎走在正確的道路上,但是根據您提供的表結構,max()函數中的case語句對我來說似乎沒有意義:

MAX(case when period_rating.Rating = 'P1' THEN Student END) 'P1'
  1. P1,P2,P3等值似乎在Slot_ID字段中,而不在Rating字段中。

  2. 在您的表格結構中沒有看到“ Student字段。 根據您問題的描述,您想返回該列中的“ Rating字段。

  3. 我將在每個case語句的else分支中放置一個明確的null

總體注釋:如果您在輸出中的NameStudent_UPN字段之后,則將select列表中的*替換為這兩列,並將它們也按列表列出。

好的,最后我通過使用以下查詢到達了那里:

SELECT seclusion_status.ID, seclusion_status.Arrived, seclusion_status.FTE, seclusion_status.Rebuild, seclusion_status.Text, seclusion_status.DateTimeAdded, seclusion_status.Staff, seclusion_status.Student_UPN, seclusion_status.Incident, seclusion_status.Comment, students.Name, students.UPN, students.Year, students.House, period_rating.ID, period_rating.Slot_ID, period_rating.Rating, period_rating.Date, period_rating.Seclusion_ID,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P1' THEN period_rating.Rating ELSE NULL END)) AS Period1_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P2' THEN period_rating.Rating ELSE NULL END)) AS Period2_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'LF' THEN period_rating.Rating ELSE NULL END)) AS LF_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'BR' THEN period_rating.Rating ELSE NULL END)) AS BR_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P3' THEN period_rating.Rating ELSE NULL END)) AS Period3_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P4' THEN period_rating.Rating ELSE NULL END)) AS Period4_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'LC' THEN period_rating.Rating ELSE NULL END)) AS LC_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P5' THEN period_rating.Rating ELSE NULL END)) AS Period5_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P6' THEN period_rating.Rating ELSE NULL END)) AS Period6_Rating,
            GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'DT' THEN period_rating.Rating ELSE NULL END)) AS DT_Rating
        FROM Seclusion_Status
        INNER JOIN Students
            ON Seclusion_Status.Student_UPN=Students.UPN
        INNER JOIN Period_Rating
            ON Seclusion_Status.ID=period_rating.Seclusion_ID
        WHERE period_rating.Date = '$start'
        GROUP BY period_rating.Seclusion_ID
        ORDER BY Seclusion_Status.DateTimeAdded ASC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM