SQL语句IN条件返回错误结果

时间:2017-04-07 15:52:34

标签: sql oracle comparison-operators

Oracle:11g

我有以下(2)个查询来评估相同的数据但返回不同的结果。

查询1:

SELECT P.PLANT_CODE, 
                      P.DESCRIPTION AS PLANT_DESC, 
                      E.EMP_ID, 
                      E.DIVISION_CODE, 
                      M.MODULE_CODE, 
                      C.COURSE_CODE 
            FROM PLANT P, 
                     EMPLOYEE E, 
                     COURSES C, 
                     MODULES_REVISION M, 
                     TEMP_QUAL_SAVED_RESULT TQSR 
         WHERE E.PLANT_CODE = P.PLANT_CODE 
             AND TQSR.EMP_ID = E.EMP_ID 
             AND TQSR.COURSE_CODE = C.COURSE_CODE 
             AND TQSR.MODULE_CODE = M.MODULE_CODE 
             AND M.CURRENT_REVISION = 'Y' 
             AND C.COURSE_STATUS = 'A' 
             AND TQSR.EMP_STATUS = 'A' 
             AND E.PLANT_CODE IN ('002088', '002096', '002105', '009247') 
             AND M.MODULE_STATUS = 'A' 
             AND E.DIVISION_CODE IN (SELECT UD.DIVISION_CODE FROM USER_DIVISION UD WHERE UD.USER_ID = 'GMD-AD-02') 
             AND QUAL_TYPE = 'M' 
     ORDER BY P.PLANT_CODE,E.EMP_ID,M.MODULE_CODE,C.COURSE_CODE; 

enter image description here 查询:2

SELECT P.PLANT_CODE, 
                      P.DESCRIPTION AS PLANT_DESC, 
                      E.EMP_ID, 
                      E.DIVISION_CODE, 
                      M.MODULE_CODE, 
                      C.COURSE_CODE 
            FROM PLANT P, 
                     EMPLOYEE E, 
                     COURSES C, 
                     MODULES_REVISION M, 
                     TEMP_QUAL_SAVED_RESULT TQSR 
         WHERE E.PLANT_CODE = P.PLANT_CODE 
             AND TQSR.EMP_ID = E.EMP_ID 
             AND TQSR.COURSE_CODE = C.COURSE_CODE 
             AND TQSR.MODULE_CODE = M.MODULE_CODE 
             AND M.CURRENT_REVISION = 'Y' 
             AND C.COURSE_STATUS = 'A' 
             AND TQSR.EMP_STATUS = 'A' 
             AND E.PLANT_CODE IN ('002088', '002096', '002105', '009247') 
             AND M.MODULE_STATUS = 'A' 
             AND E.DIVISION_CODE IN ('UTN1','UNC1','BCU2','DEU6') 
             AND QUAL_TYPE = 'M' 
     ORDER BY P.PLANT_CODE,E.EMP_ID,M.MODULE_CODE,C.COURSE_CODE;

enter image description here

Query1结果在plant_code列下显示相同的值(009247),即使它为其他(3)列带来了员工。

Query2结果在每个员工的plant_code列下显示正确的值。

  1. 为什么Query1在plant_code下只显示一个值,即使它为员工带来另一个包含在('002088','002096','002105','009247')中的plant_code?

  2. 运行子查询“SELECT UD.DIVISION_CODE FROM USER_DIVISION UD WHERE UD.USER_ID ='GMD-AD-02'”返回值'UTN1','UNC1','BCU2','DEU6'与那些来自师名单。

  3. 使用select语句指示值列表时有什么区别?

0 个答案:

没有答案