1

我有以下代码可以执行我想要的操作。但是,我需要第二个条件,其中表项目中接受的列等于是。我不知道如何将它输入到我当前的连接中,因为用户表的位置。

$query_Name = "SELECT u.Student_Surname, u.Student_Forename, Project_Title, Project_id
FROM users u
INNER JOIN projects p2 ON u.id = p2.Project_Lecturer
INNER JOIN project_course pc ON p2.Project_id = pc.Proj_id
INNER JOIN courses c ON pc.Cour_id = c.Course_id
INNER JOIN users u2 ON c.Code = u2.Course
WHERE u2.Username = ".$_SESSION['MM_Username']." 
ORDER BY Project_id ASC" ;
4

4 回答 4

1
WHERE u2.Username = ".$_SESSION['MM_Username']."  AND p2.Accepted = 'yes'

或者你可以使用

INNER JOIN projects p2 ON u.id = p2.Project_Lecturer AND p2.Accepted = 'yes'

第二种选择可能更快,但取决于其他事情

于 2012-05-27T00:51:49.240 回答
1
$query_Name = "SELECT u.Student_Surname, u.Student_Forename, Project_Title, Project_id  
 FROM users u
 INNER JOIN projects p2 ON u.id = p2.Project_Lecturer
 INNER JOIN project_course pc ON p2.Project_id = pc.Proj_id
 INNER JOIN courses c ON pc.Cour_id = c.Course_id
 INNER JOIN users u2 ON c.Code = u2.Course
 WHERE u2.Username = ".$_SESSION['MM_Username']." AND p2.Accepted = 'yes'
 ORDER BY Project_id ASC" ;
于 2012-05-27T00:48:41.343 回答
0

AND在 where 子句中添加运算符。

$query_Name = "SELECT u.Student_Surname, 
                      u.Student_Forename, 
                      Project_Title, 
                      Project_id  
               FROM users u INNER JOIN projects p2 
                       ON u.id = p2.Project_Lecturer
                    INNER JOIN project_course pc 
                       ON p2.Project_id = pc.Proj_id
                    INNER JOIN courses c 
                       ON pc.Cour_id = c.Course_id
                    INNER JOIN users u2 ON c.Code = u2.Course
               WHERE u2.Username = ".$_SESSION['MM_Username']." 
                     AND p2.Accepted = 'yes'
               ORDER BY Project_id ASC" ;
于 2012-05-27T02:25:25.120 回答
0

WHERE 实际上适用于所有连接的表,因此您在 WHERE 子句中明确说明您正在使用哪些表,否则如果查询不明确,则查询将无法工作。

所以,你使用:

WHERE
u2.Username = ".$_SESSION['MM_Username']." -- this is for the u2 table
AND
p2.Accepted = 'yes' -- this is for p2 table

您只需使用 AND 在此处添加更多逻辑。因此,整个查询将是:

$query_Name = "SELECT u.Student_Surname, u.Student_Forename, Project_Title, Project_id  
 FROM users u
 INNER JOIN projects p2 ON u.id = p2.Project_Lecturer
 INNER JOIN project_course pc ON p2.Project_id = pc.Proj_id
 INNER JOIN courses c ON pc.Cour_id = c.Course_id
 INNER JOIN users u2 ON c.Code = u2.Course
 WHERE u2.Username = ".$_SESSION['MM_Username']." AND p2.Accepted = 'yes'
 ORDER BY Project_id ASC" ;
于 2012-05-27T01:05:22.240 回答