我试图在另一个查询中使用内部联接的结果,并且无法理解如何。
这是我正在运行的第一个查询:
SELECT projects.Project_ID, projects.Name
FROM projects
INNER JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID AND assigned_users.User_ID=4;
这是为特定用户获取所有已分配的项目。 Project_ID的此查询返回我想用于查找这些项目的所有相关要求。
SELECT *
FROM requirements
WHERE requirements.Project_ID=1;
因此,我没有找到项目“1”的要求,而是希望获得分配给特定用户的所有项目的要求。任何帮助,将不胜感激。
干杯
答案 0 :(得分:0)
如果我理解正确,您只需添加另一个IS NULL
:
JOIN
答案 1 :(得分:0)
您可以使用:
SELECT *
FROM requirements req
INNER JOIN projects pro
ON req.Project_ID = pro.Project_ID
INNER JOIN assigned_users u
ON u.Project_ID=pro.Project_ID
WHERE u.User_ID=4;
或
SELECT *
FROM requirements
WHERE requirements.Project_ID=(
SELECT Project_ID
FROM projects
INNER JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID AND assigned_users.User_ID=4;
);
答案 2 :(得分:0)
对于现有查询,我们只需添加:
JOIN requirements
ON requirements.Project_ID = projects.Project_ID
我们在SELECT列表中添加表达式,以从需求中检索列的值。
SELECT projects.Project_ID, projects.Name
, requirements.some_column
, requirements.some_other_column
我们可能还想添加一个ORDER BY
子句,以便以可预测的顺序返回行。
请注意,如果给定requirements
的{{1}}中没有匹配的行,则不会返回该项目。要返回这些行,我们可以指定外部联接。
当前查询:
Project_ID
使用外部联接到SELECT projects.Project_ID
, projects.Name
FROM projects
JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID
AND assigned_users.User_ID=4
表的建议查询:
requirements
等效地,我们可以在SELECT projects.Project_ID
, projects.Name
, requirements.Requirement_ID
, requirements.Requirement_Name
FROM projects
JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID
AND assigned_users.User_ID=4
LEFT
JOIN requirements
ON requirements.Project_ID = projects.Project_ID
ORDER
BY projects.Project_ID
, requirements.Requirement_ID
之前将条件assigned_users.User_ID=4
从内连接的ON
子句重新定位到WHERE
子句。