查找记录的子集或查找全部

发布于 2025-01-25 18:27:38 字数 1366 浏览 2 评论 0原文

I'm working on a similar problem to 检查列是否包含另一列的所有值-MySQL

此CTE是较大查询的一部分。 CTE_PROJEKTEROLLER包含ProJektParamRolle的子集,可以包含零或更多记录。我想要一个projektparam的列表,其中cte_projekterollen中的项目都存在(与projektparamrolle一起使用时)。在CTE_PROJEKTROLLEN并非空的所有情况下,我的解决方案都可以使用。

CTE_FilteredByRolle as (
    select pp.ID_ProjektParam
    from Basis.ProjektParam pp
    join Basis.ProjektParamRolle ppr
        on pp.ID_ProjektParam = ppr.ID_ProjektParam
    join CTE_ProjektRollen pr
        on ppr.Rolle = pr.Rolle
    group by pp.ID_ProjektParam
    having Count(pp.ID_ProjektParam) = (
        select Count(Rolle)
        from CTE_ProjektRollen))

如果CTE_PROJEKTROLLEN为空,我需要更改以获取所有Projektparam(与Projektparamrolle一起)?

编辑:我认为我的问题错了,因为我不完全理解它。 @Kendle的解决方案适用于我所描述的内容,但实际上我需要所有ID_PROJEKTPARAM(未与Projektparamrolle一起)。

对我有用的实际CTE是

CTE_FilteredByRolle as (
    select pp.ID_ProjektParam
    from Basis.ProjektParam pp
    where (
        select Count(ppr.Rolle)
        from Basis.ProjektParamRolle ppr
        join CTE_ProjektRollen pr
            on ppr.Rolle = pr.Rolle
        where ppr.ID_ProjektParam = pp.ID_ProjektParam) = (
            select Count(Rolle)
            from CTE_ProjektRollen))

I'm working on a similar problem to check if a column contains ALL the values of another column - Mysql

This CTE is part of a bigger query. CTE_ProjekteRollen contains a subset of ProjektParamRolle and can contain zero or more records. I want a list of ProjektParam where the items in CTE_ProjekteRollen are all present (when joined with ProjektParamRolle). My solution works in all cases where CTE_ProjektRollen is not empty.

CTE_FilteredByRolle as (
    select pp.ID_ProjektParam
    from Basis.ProjektParam pp
    join Basis.ProjektParamRolle ppr
        on pp.ID_ProjektParam = ppr.ID_ProjektParam
    join CTE_ProjektRollen pr
        on ppr.Rolle = pr.Rolle
    group by pp.ID_ProjektParam
    having Count(pp.ID_ProjektParam) = (
        select Count(Rolle)
        from CTE_ProjektRollen))

What do I have to change to get all ProjektParam (joined with ProjektParamRolle), if CTE_ProjektRollen is empty?

Edit: I think I phrased my question wrong, because I didn't understand it fully. @Kendle's solution works for what I described, but I actually needed all ID_ProjektParam (not joined with ProjektParamRolle).

The actual CTE that worked for me was

CTE_FilteredByRolle as (
    select pp.ID_ProjektParam
    from Basis.ProjektParam pp
    where (
        select Count(ppr.Rolle)
        from Basis.ProjektParamRolle ppr
        join CTE_ProjektRollen pr
            on ppr.Rolle = pr.Rolle
        where ppr.ID_ProjektParam = pp.ID_ProjektParam) = (
            select Count(Rolle)
            from CTE_ProjektRollen))

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

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

发布评论

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

评论(1

悸初 2025-02-01 18:27:38

我们可以使用案例检查表是否为空。如果是空的,我们返回要进行比较的数字,因此它将始终是正确的。

CTE_FilteredByRolle as (
    select pp.ID_ProjektParam
    from Basis.ProjektParam pp
    join Basis.ProjektParamRolle ppr
        on pp.ID_ProjektParam = ppr.ID_ProjektParam
    join CTE_ProjektRollen pr
        on ppr.Rolle = pr.Rolle
    group by pp.ID_ProjektParam
    having Count(distinct pp.ID_ProjektParam) 
        = case when(select Count(distinct Rolle) from CTE_ProjektRollen)) = 0 
               then Count(distinct pp.ID_ProjektParam) 
               else (select Count(distinct Rolle) from CTE_ProjektRollen))
               end;

We can use a CASE to check whether the table is empty. If it is empty we return the number to which we are comparing, so it will always be true.

CTE_FilteredByRolle as (
    select pp.ID_ProjektParam
    from Basis.ProjektParam pp
    join Basis.ProjektParamRolle ppr
        on pp.ID_ProjektParam = ppr.ID_ProjektParam
    join CTE_ProjektRollen pr
        on ppr.Rolle = pr.Rolle
    group by pp.ID_ProjektParam
    having Count(distinct pp.ID_ProjektParam) 
        = case when(select Count(distinct Rolle) from CTE_ProjektRollen)) = 0 
               then Count(distinct pp.ID_ProjektParam) 
               else (select Count(distinct Rolle) from CTE_ProjektRollen))
               end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文