我需要在表格中将值设置为1,其中另一个表格中给定角色的计数为> 1。
表cursillo.members有memid和hsd字段。表cursillo.teams有字段memid(两者之间的链接)一个名为movement的字段和另一个名为role的字段。
我想要完成的是类似的事情:
update cursillo.members_eligible p, cursillo.teams pp
set p.hsd=1
where p.memid = pp.memid AND (pp.role = 'HSD' OR pp.role = 'SD')
and pp.movement = 'Cursillo' AND count(pp.role) > 1;
或者这个:
update members_eligibile
set hsd=1
from teams
where teams.memid=members_eligible.memid
and (teams.role = 'HSD' OR teams.role = 'SD')
and teams.movement = 'Cursillo'
and count(teams.role) > 1;
换句话说,如果给定的memid在cursillo.teams表中有多个记录,其中角色的值等于HSD或SD,则将cursillo.members_eligible.hsd设置为1.
我无法理解处理count()部分。
谢谢, 迈克里德
答案 0 :(得分:1)
可能重复的问题:
MySQL - Using COUNT(*) in the WHERE clause
尝试使用'关键字
以下是链接答案的相关部分(在本例中为select语句):
select gid
from `gd`
group by gid
having count(*) > 10
order by lastupdated desc
看起来好像有'只能用于选择的语句: http://www.mysqltutorial.org/mysql-having.aspx
因此,您可能需要更新其子句包含select语句:
update members_eligibile
set hsd=1
from teams
where teams.memid=members_eligible.memid
and (teams.role = 'HSD' OR teams.role = 'SD')
and teams.movement = 'Cursillo'
and members_eligible.memid IN
(SELECT members_eligible.memid from members_eligible where teams.memid=members_eligible.memid having count(teams.role) > 1);
你必须调整select语句,我还没有测试过它
答案 1 :(得分:0)
SQL Server 2005 +
UPDATE x
SET x.hsd = 1
FROM (SELECT e.hsd, COUNT(*) OVER (PARTITION BY t.role) AS cnt
FROM teams t JOIN members_eligibile e ON t.memid = e.memid
WHERE (t.role = 'HSD' OR t.role = 'SD') AND t.movement = 'Cursillo') x
WHERE x.cnt > 1