我目前有两个单独的SQL JOINS。一个是获取项目名称,另一个是来自同一ID的两个不同表的类名。
一个名为PUPILPROJECT的表包含一个ID,projectID和ClassID,它们也在PROJECT表中,带有PROJECT名称和带有CLASS名称的CLASS表。
有没有办法通过单个查询执行此操作?非常感谢所有帮助!
//GET PROJECT NAME
$getproject = mysql_query("SELECT project_name FROM projects JOIN pupilproject USING (project_id) WHERE pupil_project_id = '".$id."'");
while ($row = mysql_fetch_array($getproject)) {
$thisproject = $row['project_name'];
}
//GET PROJECT CLASS NAME
$classname = mysql_query("SELECT class_name FROM class JOIN pupilproject USING (class_id) WHERE pupil_project_id = '".$id."'");
while ($row = mysql_fetch_array($classname)) {
$pclass = $row['class_name'];
}
答案 0 :(得分:0)
在代码中使用以下SQL:
"SELECT project_name, class_name
FROM pupilproject
LEFT JOIN projects USING (project_id)
LEFT JOIN class USING (class_id)
WHERE pupil_project_id = '".$id."'"