怎么得到某字段最大的记录.在线等待,当场结分.

klggg 2002-10-12 05:18:03
表PactRate

id PactId GatheringYear PactCost
1 1 1999 12000
2 1 2000 12000
3 3 1002 12000
4 4 2002 50000
5 5 2002 100

运行 mysql语句
select * from PactRate group by PactId
得到
id PactId GatheringYear PactCost
1 1 1999 12000 *
3 3 1002 12000
4 4 2002 50000
5 5 2002 100
但我想要得到这样的结果
id PactId GatheringYear PactCost
2 1 2000 12000 *
3 3 1002 12000
4 4 2002 50000
5 5 2002 100

也就是说 得 存在PactId重复的记录时取GatheringYear 最大的那个.
应该怎么做!
在线等待.
...全文
69 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hezhiqiang 2002-10-12
  • 打赏
  • 举报
回复
这样:
  select id, PactId, max(GatheringYear), PactCost from PactRate group by PactId

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧