首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用两个while循环的PHP查询

使用两个while循环的PHP查询
EN

Stack Overflow用户
提问于 2017-03-24 11:10:34
回答 1查看 49关注 0票数 0

我的问题是表数据中的名字与表数据中的第二个名字相同

我想要的是第一个表数据名字应该是学生的名字,第二个表数据名字应该是老师的名字

假设我有多个数据,所以我使用了While循环。

示例数据:

学生是John Doe和Mike Gard

老师是麦卡和杰西

代码语言:javascript
复制
1st record: John Doe  Erase the Board  Done   Myka
2nd record: Mike Gard Erase the Board  Done   Myka (This should be Jess)

校表(教师或学生)字段: personid,firstname,lastname

任务表字段: personid、task、status、teacherid ->此字段来自人员id

代码语言:javascript
复制
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>
    }   
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-24 11:32:07

如果我理解正确的话,我不认为你需要两个单独的查询。

代码语言:javascript
复制
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'teachername’来代替$row1'firstname‘和$row2'firstname’。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42990763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档