Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择具有一对多条件的所有行_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 选择具有一对多条件的所有行

Sql 选择具有一对多条件的所有行,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想选择tblAAA中满足条件的所有行 tblAAA有一个或多个tblBBB。 一个tblBBB有一个tblCCC。 我想更新tblAAA中的所有行,其中其所有tblBBB都有一个tblCCC,其中tblCCC.status=1 请参阅提供的图片 非常感谢你的帮助,我已经盯着这个看了两个小时,但是没有任何线索 编辑: 我试过的一件事是: select * from tblAAA inner join tblBBB on tblAAA.tblAAA_id = tblBBB.tblAAA_id in

我想选择tblAAA中满足条件的所有行

tblAAA有一个或多个tblBBB。 一个tblBBB有一个tblCCC。 我想更新tblAAA中的所有行,其中其所有tblBBB都有一个tblCCC,其中tblCCC.status=1

请参阅提供的图片

非常感谢你的帮助,我已经盯着这个看了两个小时,但是没有任何线索

编辑: 我试过的一件事是:

select * from tblAAA
inner join tblBBB
on tblAAA.tblAAA_id = tblBBB.tblAAA_id
inner join tblCCC
on tblBBB.tblCCC_id = tblCCC.tblCCC_id
where tblCCC.status = 1;
但这不起作用,因为这会给出至少一个tblBBB满足条件的所有tblAAA


如果图表表示返回来自
tblAAA
的所有行,这些行在
tblBBB
中有一行,但没有一行导致
tblCCC.active=0
,则:

雷克斯测试仪:

更新所有
tblAAA
,其中所有
tblBBB
都有
tblCCC
其中
tblCCC.active=1

update a
  set a.status=1
  from tblAAA as a
    inner join tblBBB as b on a.AAA_id = b.AAA_id
  where not exists (
    select 1
      from tblBBB as b
        inner join tblCCC as c on b.CCC_id=c.CCC_id
        where b.AAA_id = a.AAA_ID
          and c.active=0
    )


如果你能告诉我你为什么不投反对票,我将不胜感激,但我想有人认为你好像想让我们做你的家庭作业。好吧,那就错了。我想了解如何去做,并且自己学习如何去做。我可以发布我尝试过的东西,但我觉得这会让你感到困惑。你的问题让人困惑,图表也帮不上什么忙。最好发布一些示例数据,并描述您正在寻找的结果。请向我们展示您的尝试。否则,这个问题看起来像“给我一个codez!”在没有使用聚合函数的情况下为什么要分组?@jarlh与在本例中使用
select distinct
相同。导致计数(b.Id)的意外情况取决于图表的解释方式。我将对此进行测试,稍后再与您联系。谢谢
update a
  set a.status=1
  from tblAAA as a
    inner join tblBBB as b on a.AAA_id = b.AAA_id
  where not exists (
    select 1
      from tblBBB as b
        inner join tblCCC as c on b.CCC_id=c.CCC_id
        where b.AAA_id = a.AAA_ID
          and c.active=0
    )
update a
  set a.status=1
  from tblAAA as a
  where not exists (
    select 1
      from tblBBB as b
        inner join tblCCC as c on b.CCC_id=c.CCC_id
        where b.AAA_id = a.AAA_ID
          and c.active=0
    )
    and exists (
      select 1
        from tblBBB as b
          where b.AAA_id = a.AAA_ID
          )
update tblAAA
set status = 'new status'
where AAA_ID in (
    select a.AAA_ID
    from tblAAA a
    join tblBBB b on a.AAA_ID = b.AAA_ID
    join tblCCC c on c.CCC_ID = b.CCC_ID
    group by a.AAA_ID, a.status
    having sum(case c.active when 1 then 1 else 0 end) = count(c.active)
)