0

我使用 Firebird 3.0,我有 3 个表:

表1:tbl1_id(PK)、f2_id(FK)、tbl1_f1

tbl1_f2 是 table2 的外键

表2:f2_id(PK)、f3_id(FK)

f3_id 是 table3 的外键

表3:f3_id(PK)、tbl3_code

现在我需要设置 Table1.tbl1_f1 = 1 其中 Table3.tbl3_code = 'value' 所以我写了这个 SQL:

update table1 set tbl1_f1 = 1 where (tbl1_f1 is null)
and table1.tbl1_id in (

select
    tbl1_id

from table1 
   inner join Table2 on (Table1.f2_id = Table2.f2_id)
   inner join Table3 on (Table2.f3_id = Table3.f3_id)

where (Table3.tbl3_code = 'value')

 )

我的更新 SQL 是正确的还是有更好的编写方法?

4

1 回答 1

1
execute block
as
declare id bigint;
begin
for select tbl1_id
from table1 
   inner join Table2 on (Table1.f2_id = Table2.f2_id)
   inner join Table3 on (Table2.f3_id = Table3.f3_id)
where (Table3.tbl3_code = 'value')
into :id
do update table1 set tbl1_f1 = 1 where (tbl1_f1 is null)
and table1.tbl1_id =:id;

end;
于 2016-07-12T18:41:15.227 回答