2017-01-30 2 views
-2

Я хочу выбрать все строки в tblAAA, которые удовлетворяют условию.Выделить все строки с условием «один ко многим»

A tblAAA имеет 1 или больше tblBBB. A tblBBB имеет 1 tblCCC. Я хочу, чтобы обновить все строки в tblAAA где все его tblBBB имеет tblCCC где tblCCC.status = 1.

Пожалуйста, см при условии изображения.

Действительно благодарен за помощь, я смотрел на это в течение двух часов, но понятия не имею.

Edit: Одна вещь, которую я сделал попытку заключалась в следующем:

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; 

Но это не работает, потому что это дает все tblAAA, где по крайней мере один tblBBB удовлетворяет условию.

enter image description here

+0

бы признателен, если вы скажете мне, почему ты downvote – hellogoodnight

+0

я не downvote (пока), но я полагаю, кто-то думает, что это выглядит, как вы хотите, чтобы мы, чтобы сделать ваши домашнее задание. – jarlh

+0

Хорошо, это было бы неправильно. Я хочу понять, как это сделать, и научиться самому это делать. Я мог бы опубликовать материал, который я пробовал, но я чувствую, что это просто смутит вас. – hellogoodnight

ответ

1

Если диаграмма говорит, чтобы вернуть все строки из tblAAA, которые имеют ряд в tblBBB, но не имеют ряд, которые приводят к tblCCC.active= 0, то:

rextester: http://rextester.com/OBOE63409

create table tblAAA (AAA_Id int, status bit); 
insert into tblAAA values (1,1),(2,0),(3,1); 

create table tblBBB (BBB_id int identity(1,1), AAA_id int, CCC_id int); 
insert into tblBBB values (1,1),(2,1),(2,2); 

create table tblCCC (CCC_id int, active bit); 
insert into tblCCC values (1,1),(2,0); 

/* AAA_Id 1 has only one row in tblBBB and leads to tblCCC.active=1 */ 
/* AAA_Id 2 has only two rows in tblBBB and one leads to tblCCC.active=0 */ 
/* AAA_Id 3 has no row in tblBBB -- not returned */ 

/* using the inner join from tblAAA to tblBBB 
    requires that AAA_id have at least one row in tblBBB */ 

select a.AAA_id, a.status 
    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 
) 
    group by a.AAA_id, a.status 
    /* if AAA_id must have at least n rows in tblBBB 
    that lead to tblCCC.active=1 
    then using having count(b.Id) > n */ 
    --having count(b.Id)>1 

Обновить все 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 
    ) 

или

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 
     ) 
+0

Почему GROUP BY, когда не используются агрегированные функции? – jarlh

+0

@jarlh так же, как использовать 'select distinct' в этом случае. Вводит непредвиденные обстоятельства для 'count count (b.Id)' в зависимости от того, как должна интерпретировать диаграмма. – SqlZim

+0

Я проверю это, скоро вернусь к вам. Спасибо – hellogoodnight

0
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) 
) 
Смежные вопросы