2017-03-24 10 views
0

私の問題は、テーブルのデータでFIRSTNAMEがPHPクエリーwhileループ

何が起こるしたいことは最初のテーブルのデータFIRSTNAMEが学生でなければならないことである表データにおける第二ファーストネームと同じであるということです名前と2番目のテーブルのデータfirstnameは教師の名前にしてください

私はWhileループを使用したので、私は複数のデータを持っているとしましょう。

サンプルデータ:

学生ジョン・ドウとマイク・ガール

教師はMykaとジェス

1st record: John Doe Erase the Board Done Myka 
2nd record: Mike Gard Erase the Board Done Myka (This should be Jess) 

スクール表(教師または生徒) フィールドですされています PERSONID、 FIRSTNAME、 姓

タスクテーブル フィールド: PERSONID、 タスク、 状況、 teacherid - >これは私が正しく理解していた場合、私はあなたがこのために2つの別々のクエリを必要としないと思う人のID

sql1 = select * from School S Inner Join Task T on S.personid = T.personid // I want to get the ID of the students to get the names 
sql2 = select * from School S Inner Join Task T on S.personid = T.teacherid // I want to get the ID of the teacher to get the names 

<th>Student Name</th> 
<th>Task</th> 
<th>Teacher Name</th> 
<th>Status</th > 

$result1 = mysqli_query($conn, $sql1); 
$result2 = mysqli_query($conn, $sql2); 

while ($row1 = mysqli_fetch_array($result1)) { 
    while ($row2 = mysqli_fetch_array($result2)){ 

     <td>".$row1['firstname']."</td>//Name of the student 
     <td>".$row1['task']."</td> 
     <td>".$row2['firstname']."</td>//Name of the teacher 
     <td>".$row1['result']."</td> 
    } 
} 
+0

データセットがわからないと、ループ1がすべての人を選択するということを予測するのは難しいです。ループ2はすべての教師を選択します。だから、各Personsには、すべての教師を表示します。だからPerson Xの教師=(Large Dataset)学校の最初の人が先生の場合、最初の結果は常に同じ人になります。 – Forbs

答えて

1

から来ました。

SELECT T.*, 
S1.firstname AS studentname, 
S2.firstname AS teachername 
FROM Task T 
LEFT JOIN School S1 ON T.personid=S1.personid 
LEFT JOIN School S2 ON T.teacherid=S2.personid; 

今、あなただけのものを使用することができますwhileループと参照の$ ROW1 [ 'studentname']と$ ROW1 $ ROW1 [ 'ファーストネーム']と$ ROW2の代わりに[ 'teachername'] [ 'firstnameの'] 。

+0

ありがとうございました病気ありがとうございました。 – questioner