提供记录的SQL查询甚至不存在

时间:2016-03-20 18:12:47

标签: mysql sql-server

我有这个SQL查询:

SELECT project.id, project.title project_title, project.approval_status, office.office office_name, project.date_submitted, project.reason_for_rejection
FROM project
JOIN user ON project.user_id = user.id
JOIN office ON user.office_id = office.id
WHERE EXISTS
(
        SELECT t1.id id1, t2.id id2, t3.id id3, t4.id id4, t5.id id5
        FROM user t1
        LEFT JOIN user t2 ON t2.reports_to = t1.id
        LEFT JOIN user t3 ON t3.reports_to = t2.id
        LEFT JOIN user t4 ON t4.reports_to = t3.id
        LEFT JOIN user t5 ON t5.reports_to = t4.id
        WHERE t1.id = (
            SELECT id
            FROM user
            WHERE office_id = 52
            AND reports_to = 22
        )
)

这是查询返回的记录

enter image description here

但是我只想要查询中存在的记录

SELECT t1.id id1, t2.id id2, t3.id id3, t4.id id4, t5.id id5
FROM user t1
LEFT JOIN user t2 ON t2.reports_to = t1.id
LEFT JOIN user t3 ON t3.reports_to = t2.id
LEFT JOIN user t4 ON t4.reports_to = t3.id
LEFT JOIN user t5 ON t5.reports_to = t4.id
WHERE t1.id = (
    SELECT id
    FROM user
    WHERE office_id = 52
    AND reports_to = 22
)

在我运行时提供此记录

enter image description here

我只想要此查询中存在project.user_id的记录。

我的代码出了什么问题?

0 个答案:

没有答案