我想删除以下子查询的结果。我怎么做?
delete
from mytable
where rowid in (select rowid, count(*) as count from mytable group by mygroup having count > 50)
不起作用:
[1] [SQLITE_ERROR] SQL错误或缺少数据库(子选择返回8 栏-预期1)
答案 0 :(得分:1)
delete
from mytable
where rowid in (select rowid from (select rowid, count(*) as count from mytable group by mygroup having count > 50))
答案 1 :(得分:1)
我猜您在使用聚合函数时可能需要这样做,您需要在group by
中添加非聚合列
delete
from mytable
where mygroup in (select mygroup from mytable group by mygroup having count(*) > 50)
答案 2 :(得分:1)
如果选中此page,您会注意到,在使用IN
和子查询时:
值列表是固定值列表或子查询返回的结果一列。返回的表达式类型和列表中的值必须相同。
您在这里的错误是您的子查询返回的列超过1(行和计数)。
要解决此问题,您可以尝试以下操作:
delete
from mytable
where rowid in (select rowid
from (select rowid,
count(*) as count
from mytable
group by mygroup
having count > 50)
)
请注意,假设您要删除所有mygroup计数大于50的结果,此查询将无法完成全部工作。它只会返回该组的一行。您应该尝试这样做:
select * FROM mytable
where mygroup in (select mygroup
from (select mygroup,
count(*) as count
from mytable
group by mygroup
having count > 1)
);
如D-Shish's answer中所述。
这里是demo,用于查看这两个查询的两个不同结果集。