我有三個表:加入3個表格,不輸出第三個表格的所有結果?
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_status表與Seclusion_ID
許多條目我正在運行以下查詢以基於日期從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'
GROUP BY period_rating.Seclusion_ID
ORDER BY Seclusion_Status.DateTimeAdded ASC";
$result=mysql_query($sql);
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
該查詢返回Seclusion_Status記錄,並在Period_rating然後第一條記錄,而不是其他。
Array
[0] => 348
[ID] => 157
[1] => Y
[Arrived] => Y
[2] => N
[FTE] => N
[3] =>
[Rebuild] =>
[4] =>
[Text] =>
[5] => 2016-03-04 09:30:50
[DateTimeAdded] => 2016-03-04 09:30:50
[6] => Mr S Holland
[Staff] => Mr S Holland
[7] => K80222800
[Student_UPN] => K8022280
[8] => Refusing instructions
[Incident] => Refusing instructions
[9] =>
[Period] =>
[10] =>
[Period_In_ID] =>
[11] => Not sitting properly in class despite being asked
[Comment] => Not sitting properly in class despite being asked
[12] => K80222800
[UPN] => K80222800
[13] => Student Name
[Name] => Student Name
[14] => Year 9
[Year] => Year 9
[15] => Acer
[House] => Acer
[16] => 157
[17] => P2
[Slot_ID] => P2
[18] =>
[Rating] =>
[19] => 2016-03-04
[Date] => 2016-03-04
[20] => 348
[Seclusion_ID] => 348
[21] => 1
[Status] => 1
好的。我已經刪除了這個聲明,並且這種解決了這個問題。它現在正在返回所有的數據。我將這些數據寫入表格的行中,並且每個學生需要一個單獨的行,但是在同一行中爲該學生記錄所有的period_rating記錄。在它循環的那一刻,它爲Period_rating中的每條記錄在單獨的行上寫入同一個學生。 – user3122230
對不起,但這是與原來不同的問題。如果這個答案幫助你解決你的問題,那麼請接受答案,並在他們自己的話題中提出任何後續問題! – Shadow