[英]UPDATE SQL table based on query
I have a table called students
with 1000 students in. I have a query which tells me which of those students has free tuition. 我有一个叫“
students
的表格,其中有1000名学生。我有一个查询,告诉我其中哪些学生有免费学费。 In the stduents
table I have a field called FreeTuition
and I want to populate/update that field with the results of the query. 在
stduents
表中,我有一个名为FreeTuition
的字段,我想用查询结果填充/更新该字段。 Do I need to use some kind of loop? 我需要使用某种循环吗?
The students
table has StuCode which is unique, the query returns StuCode of all the students with free tuition. students
表具有唯一的StuCode,查询将免费返回所有学生的StuCode。 This is how I want it to look: 这就是我想要的样子:
| StuCode | FreeTuition |
-------------------------
| S12345 | Yes |
| S12346 | No |
-------------------------
Not at all. 一点也不。 Something like this:
像这样:
with yourquery as (
<your query here>
)
update s
set FreeTuition = (case when yq. StuCode is not null then 'Y' else 'N' end)
from students s left join
yourquery yq
on s. StuCode = yq. StuCode;
Note: This sets the value for all students, yes or no. 注意:这将为所有学生设置值,是或否。 You can change the
left join
to just join
to set the value only for students returned by the subquery. 您可以将
left join
更改为仅join
以仅为子查询返回的学生设置值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.