根据条件选择列?

发布于 2024-12-04 02:54:15 字数 414 浏览 3 评论 0原文

我有一个详细信息表“详细信息”,如下所示:

详细信息:

    ID          StatusID
   ----         --------
     1             4
     1             4
     2             4
     2             3
     3             4   
     3             4
     4             3
     4             3

在这里,我只想选择具有所有 StatusID = 4 的 ID:

我想要的结果应该如下所示:

  ID
 ----
  1
  3

如何实现这一点?

I have a Detail table "Detail" which is like below:

Detail:

    ID          StatusID
   ----         --------
     1             4
     1             4
     2             4
     2             3
     3             4   
     3             4
     4             3
     4             3

Here, I want to select only the ID which has all the StatusID = 4:

My Desired Result should be like below:

  ID
 ----
  1
  3

How to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-12-11 02:54:15

您可以使用 not contains 子查询:

select  distinct yt1.ID
from    YourTable yt1
where   yt1.StatusID = 4
        and not exists
        (
        select  *
        from    YourTable yt2
        where   yt2.StatusID <> 4
                and yt2.ID = yt1.ID
        )

You could use a not exists subquery:

select  distinct yt1.ID
from    YourTable yt1
where   yt1.StatusID = 4
        and not exists
        (
        select  *
        from    YourTable yt2
        where   yt2.StatusID <> 4
                and yt2.ID = yt1.ID
        )
不甘平庸 2024-12-11 02:54:15
select distinct ID 
from YourTable
where ID not in
(
    select ID 
    from YourTable
    where StatusID <> 4
)
select distinct ID 
from YourTable
where ID not in
(
    select ID 
    from YourTable
    where StatusID <> 4
)
草莓味的萝莉 2024-12-11 02:54:15

只是为了好玩,加入版本怎么样?

select distinct
        t.id
    from
        your_table as t
        left outer join(select
                id,
                statusid
            from
                your_table
            where
                statusid != 4
        ) as j
            on t.id = j.id
    where
        j.id is null
;

Just for fun, how about a join version

select distinct
        t.id
    from
        your_table as t
        left outer join(select
                id,
                statusid
            from
                your_table
            where
                statusid != 4
        ) as j
            on t.id = j.id
    where
        j.id is null
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文