[英]UPDATE table using IN and COUNT
我正在更新表,并根据以下条件设置一个名为“状态”的字段:不同行的总数应大于10且小于13。查询如下:
update myTable set status='Established'
where id IN(select id, count(*) as c
from myTable
where year>=1996 and year<=2008
group by id
having count(distinct year)>=10 and count(distinct year)<=13)
问题是,我收到error1241,即“操作数应包含1列”! 您能否建议我该如何解决? 谢谢!
子查询的结果必须仅返回1列:
update myTable set status='Established'
where id IN(select id
from myTable
group by id
having count(distinct year)>=10 and count(distinct year)>=13)
在MySQL中, update
了join
经常进行比更好update
的与子查询where
的条款。
此版本可能具有更好的性能:
update myTable join
(select id, count(*) as c
from myTable
where year >= 1996 and year <= 2008
group by id
having count(distinct year) >= 10 and count(distinct year) <= 13
) filter
on myTable.id = filter.id
set status = 'Established';
我还将注意到,您有一个表,其中名为id
的列在各行中不是唯一的。 通常,这样的列将是主键,因此having
子句将始终失败(只有一行)。
update myTable
set status='Established'
where id IN(select id from myTable
group by id
having count(distinct year)>=10
and count(distinct year)>=13)
您正在使用IN运算符,然后内部查询返回两列id和count(*),它应该仅返回一列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.