如何删除一组中的最高值
我正在尝试从组中删除最高值。鉴于此数据:
group_table
group_id | st_area
---------------
1 | 20
1 | 30
2 | 1
2 | 1
2 | 200
我想删除第二行和第五行。我知道如何从每个组中选择最高的:
SELECT max(area) FROM group_table GROUP BY group_id
不过,我不知道如何制定一个删除语句来接受它作为子查询。当我尝试 delete from group_table where st_area = max(st_area);
时,
出现错误:WHERE 子句中不允许聚合
。
我很快就了解到的一件事是,我不是一个非常好的沟通者。如果我问的不清楚&你很有耐心,我一定会尽力澄清。
谢谢!
I'm trying to delete the highest value from a group. Given this data:
group_table
group_id | st_area
---------------
1 | 20
1 | 30
2 | 1
2 | 1
2 | 200
I'd like to remove the second and fifth rows. I know how to select the highest from each group:
SELECT max(area) FROM group_table GROUP BY group_id
I can't figure out how to formulate a delete statement that will accept that as a subquery, though. When I try delete from group_table where st_area = max(st_area);
I get an error: aggregates not allowed in WHERE clause
.
One thing I have learned pretty quickly on SO is that I'm not a terribly good communicator. If what I'm asking is unclear & you're feeling patient, I'll certainly try to clarify.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的表中有主键吗?根据你的规范,你不需要。
在这种情况下:
但是如果您有两个最大值行,它将删除这两行。
如果在这种情况下您只想删除一个,则应该为选择添加一个 id 列
:);
因此,您要为每个组 id 的每个最大 st_area 选择最大 id。
如果您有这样的结构:
第一个查询将删除第 1 行和第 3 行,第二个查询将删除第 3 行。
Do you have some primary key in your table? According to your specification, you don't.
In this case:
But if you have TWO maximum-value rows, it will delete both.
If in this case you want to delete just one, you should add an id column for the selection:
);
So you are selecting the maximum id for every maximum st_area for every group id.
In case you have a structure like this:
the first query will delete rows 1 and 3, and the second query just 3.