SQL子查询未获取正确的数据

时间:2018-12-18 15:15:02

标签: sql

我刚刚开始一些编程课程,并且正在编写CRUD操作以获取一些数据。每当下面的子查询从合同表返回最大终止日期时,它都应返回与3个不同表中的所有关系匹配的日期。看起来子查询正在忽略where子句。我在子查询方面没有太多经验,好像我在犯一些错误,请帮忙。伙计们,我现在快完成了,我需要弄清楚如何在子查询中使用max函数,例如.... AND(MAX(employee.DT_START)<=(SELECT Max(A.DT_END)                                    从合同A ........

请提出如何为员工使用max功能。DT_START

SELECT DISTINCT employee.FIRST_NAME 
FROM   employee 
WHERE  employee.ID_EMP = 1 
       AND ( employee.DT_START <= (SELECT Max(A.DT_END) 
                                   FROM   contract A, 
                                          salary E, 
                                          department D 
                                   WHERE  A.ID_PRSN = E.ID_PRSN 
                                          AND A.ID_DEPT = D.ID_DEPT) ); 

2 个答案:

答案 0 :(得分:0)

尝试

        select distinct emp.FIRST_NAME from
                            (                                     
                            SELECT FIRST_NAME, max(DT_START) as DT_START
                            FROM   employee 
                            WHERE  ID_EMP = 1 
                            group by FIRST_NAME
                            ) emp,
                            (SELECT Max(A.DT_END) as DT_END 
                                           FROM   contract A, 
                                                  salary E, 
                                                  department D 
                                           WHERE  A.ID_PRSN = E.ID_PRSN 
                                                  AND A.ID_DEPT = D.ID_DEPT
                            ) sal
        where emp.DT_START <= sal.DT_END;

答案 1 :(得分:-1)

如果要基于3个不同表中的关系获取值,请使用JOIN。