[英]update existing table column based on condition
I am trying to write SQL query for following table, 我正在尝试为下表编写SQL查询,
Employee 雇员
id | name | batch |certificate_name | flag
---------------------------------------
1| stefan | a | java | null
2| ross | b | c++ | null
3| mad | a | php | null
Query is, for each row check occurrences of certificate and if certificate occurs once in table and batch is 'a' then make flag 1 else do nothing 查询是,对于每一行,检查证书的出现情况,如果证书在表中出现一次并且批处理为“ a”,则使标志为1,否则不做任何事情
Result should be: 结果应为:
id | name | batch |certificate_name | flag
---------------------------------------
1| stefan | a |java | 1
2| ross | b |c++ | null
3| mad | a |php | 1
what I tried is following, 我尝试的是
update employee set flag = 1
where batch = 'a'
-- how to write sql query condition for check current row certificate occurrence in table
and ???
I would first build a list of unique certificates. 我首先要建立一个唯一证书列表。
SELECT certificate_name FROM Employee GROUP BY certificate_name HAVING count(*) = 1;
Next I would use that when doing an update. 接下来,我将在进行更新时使用它。
UPDATE Employee
SET flag = 1
WHERE batch = 'a'
and certificate_name IN (SELECT certificate_name FROM Employee GROUP BY certificate_name HAVING count(*) = 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.