开发者社区> 问答> 正文

在两段SQL语句中间加NOT EXISTS语句时,无法查询获得结果

现在遇到一个问题,始终不知道如何解决。
要写的SQL语句的功能是取出某几张表中的数据不含指定表中的某几行的数据;
首先我的取出整体的记录SQL语句如下:
screenshot

查询结果集如下:
screenshot

查询该集中的部分集SQL代码如下:
screenshot

执行结果如下:
planMaker

为何我在两段SQL语句中间加NOT EXISTS语句时,就无法查询获得结果?
合起来的SQL语句如下:

screenshot

展开
收起
吴孟桥 2016-06-08 16:41:50 3008 0
2 条回答
写回答
取消 提交回答
  • not exists 是不存在则条件成立,执行前面的语句,否则该语句会被跳过

    2021-10-12 11:19:16
    赞同 展开评论 打赏
  • not EXISTS 通俗点讲,就是返回一个boolean类型 。也就是你的这个条件的意思,如果

    SELECT
        militarySuperviseQuarterSubmissionTable.fileSubmissionPersonId
    FROM
        militarySuperviseQuarterSubmissionTable,
        projectMemberRelationTable
    WHERE
        militarySuperviseQuarterSubmissionTable.fileSubmissionPersonId = projectMemberRelationTable.systemUserID
    AND projectMemberRelationTable.projectID = 22
    AND militarySuperviseQuarterSubmissionTable.militarySuperVisorUniqueId = 2

    这里有查询出来没有数据,条件才成立

    废话不多说,你想要的结果应该是这样的吧:

        SELECT
        BSPMSuser.userName,
        BSPMSuser.userDepartment,
        projectMemberRelationTable.systemUserID,
        userAuthority.authorityName,
        BSPMSuser.MemberResponsiblity,
        BSPMSuser.memberPosition,
        BSPMSuser.MemberTitle,
        BSPMSuser.memberAddress,
        BSPMSuser.userPhoneNumber,
        BSPMSuser.memberNote
    FROM
        BSPMSuser,
        userAuthority,
        militarySuperviseQuarterSubmissionTable,
        projectMemberRelationTable
    WHERE
        projectMemberRelationTable.projectID = 22
    AND projectMemberRelationTable.authorityId = userAuthority.userGroupID
    AND projectMemberRelationTable.systemUserID = BSPMSuser.userID
    AND NOT IN (
        SELECT
            militarySuperviseQuarterSubmissionTable.fileSubmissionPersonId
        FROM
            militarySuperviseQuarterSubmissionTable,
            projectMemberRelationTable
        WHERE
            militarySuperviseQuarterSubmissionTable.fileSubmissionPersonId = projectMemberRelationTable.systemUserID
        AND projectMemberRelationTable.projectID = 22
        AND militarySuperviseQuarterSubmissionTable.militarySuperVisorUniqueId = 2
    );
    2019-07-17 19:32:18
    赞同 展开评论 打赏
问答分类:
SQL
问答地址:
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载