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时
除了通过过程之外,还可以在这个查询中完成吗?
发布于 2012-09-13 22:29:37
这可以很容易地完成:
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子句中应用条件变得更容易。
https://stackoverflow.com/questions/12407291
复制相似问题