首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据查询的结果集更新另一个表

根据查询的结果集更新另一个表
EN

Stack Overflow用户
提问于 2012-09-13 21:21:12
回答 1查看 250关注 0票数 0
代码语言:javascript
运行
复制
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

这给了我一些结果

代码语言:javascript
运行
复制
count1   count2
 4         9

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

代码语言:javascript
运行
复制
 count1   count2
 9         9

我需要使用这个结果更新另一个表,比如当count1=count2更新一个名为tb_log的表并设置falg=1

除了通过过程之外,还可以在这个查询中完成吗?

EN

回答 1

Stack Overflow用户

发布于 2012-09-13 22:29:37

这可以很容易地完成:

代码语言:javascript
运行
复制
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子句中应用条件变得更容易。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12407291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档