0
select 
(SELECT count(ts.student_id) AS studentcount
from tb_student ts
where  ts.tudent_id>101 )as count1,
(SELECT count(tt.teacher_id) AS Totalteacher from tb_teacher tt
 where feedback_id<>0) as count2

这给了我一些结果

count1   count2
 4         9

在某些情况下,计数将相等,例如

 count1   count2
 9         9

我需要使用此结果更新另一个表,例如何时count1=count2 更新一个名为tb_log并设置 的表falg=1

除了通过过程进行之外,它是否可以在此查询中完成。

4

1 回答 1

0

这可以很容易地完成:

update tb_log
    set falg = 1
    where exists (select count1, count2
                  from (SELECT count(ts.student_id) AS studentcount
                        from tb_student ts
                        where  ts.tudent_id>101
                       ) as count1 cross join
                       (SELECT count(tt.teacher_id) AS Totalteacher
                        from tb_teacher tt
                        where feedback_id<>0
                       ) as count2
                  where count1 = count2
                 )

我将您的子查询从 SELECT 子句移到 FROM 子句中。我认为当表引用位于 FROM 子句中时,查询更容易理解。这也使得在 WHERE 子句中应用条件变得更加容易。

于 2012-09-13T14:29:36.543 回答