mysql where排除,MySQL'WHERE'子句排除子查询中的结果
发布日期:2022-03-15 11:49:52 浏览次数:28 分类:技术文章

本文共 1211 字,大约阅读时间需要 4 分钟。

I have the following SQL query:

SELECT members.member_ID, members.nick_name

FROM orgs

INNER JOIN assets ON assets.org_ID = orgs.org_ID

INNER JOIN orgs_to_members ON orgs_to_members.org_ID = orgs.org_ID

INNER JOIN members ON members.member_ID = orgs_to_members.member_ID

where orgs.org_ID = '7'

AND NOT EXISTS (select shares.member_ID from shares where shares.asset_ID = '224')

There are 3 members in org 7:

- member_ID 1

- member_ID 4

- member_ID 6

In the subquery, member IDs 1 and 4 result. I am trying to write 1 query which will return only member ID #6. When i run the above query, I get no results. When I separate them, I get the expected results. Please help.

Thanks!

解决方案

AND NOT EXISTS (select ...) is used to make sure that the subquery doesn't return any rows. It usually only makes sense if the subquery is correlated (i.e., if it refers to values from the outer query), since otherwise it will either be true for every result row (and won't actually affect the query), or be false for every result row (and will cause the query to return no results at all, as in your case). I think what you want is:

AND members.member_ID NOT IN (select shares.member_ID from shares where shares.asset_ID = '224')

转载地址:https://blog.csdn.net/weixin_34574003/article/details/118814125 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:css 加载 500错误,express中加载css和js时候出现500错误
下一篇:启动计算机管理服务,是哪一个启动项控制了计算机管理--服务下的wireless zero configuration无法开机自行启动!如何解决,为盼...

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月03日 11时11分09秒