我正在更新我的表设置一个名为" status"的字段。基于条件,不同行的总数应大于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)
问题是,我得到的错误1241是&#34;操作数应该包含1列&#34;!你能告诉我怎样才能解决这个问题?谢谢!
答案 0 :(得分: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)
答案 1 :(得分:1)
在MySQL中,update
join
的{{1}}通常比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
子句总是会失败(只有一行)。
答案 2 :(得分:0)
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(*)它应该只返回一列。