0

我需要根据一张表过滤数据,但我不知道 php mysql 中 select from 函数的正确用法。下面的代码显示了我正在尝试做的事情,但我认为您可以使用多个 SELECT。

$query_Student = "SELECT Project_Title, Project_Lecturer FROM projects WHERE Project_id = 
 (SELECT Proj_id FROM project_course WHERE Cour_id = (SELECT Course_id from courses WHERE     
Code = (SELECT Course FROM users WHERE Username = ".$_SESSION['MM_Username']."))"

任何人都可以对我需要写的内容提供任何帮助吗?

4

2 回答 2

2

你想要做什么对我来说并不完全清楚,但要使你的查询工作,你需要在 周围加上单引号$_SESSION['MM_Username'],就像 gogowitsch 已经说过的那样。

还缺少另一个关闭括号。所以你的查询应该是这样的:

$query_Student = "SELECT Project_Title, Project_Lecturer FROM projects WHERE Project_id = 
 (SELECT Proj_id FROM project_course WHERE Cour_id = (SELECT Course_id from courses WHERE     
Code = (SELECT Course FROM users WHERE Username = '".$_SESSION['MM_Username']."')))"

但...

这不是一个写得很好的查询。

我现在只是在猜测,但也许这就是你要找的:

SELECT 
p.Project_Title, 
p.Project_Lecturer 
FROM projects p
INNER JOIN project_course pc ON p.Project_id = pc.Proj_id
INNER JOIN courses c ON pc.Cour_id = c.Course_id
INNER JOIN users u ON c.Code = u.Course
WHERE u.Username = 'yourUserName';

If not, show us the tables you are using (do a SHOW CREATE TABLE projects; and so on for every table you are using and post it here) and maybe what output you expect, then we can really help.

于 2012-05-20T21:08:29.277 回答
0

您可能需要在用户名周围添加引号。可以$_SESSION['MM_Username']是字符串吗?

于 2012-05-20T20:37:46.147 回答