如何查询购物表中所有客户的最后一次购买记录?

itfly 2006-05-08 01:00:19
表结构
id khid gypz
1 1 2
2 1 3
3 1 2
4 1 3
5 2 1
6 2 2
7 3 2
我是想查询
凡是最后一次购买gypz=2的Khid是那些,这种SQL该怎么写?
...全文
204 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
8LY8Apollo 2006-05-08
  • 打赏
  • 举报
回复
select distinct khid from yourtable where gypz = 2
pengdali 2006-05-08
  • 打赏
  • 举报
回复
select * from 表 a where exists(select 1 from 表 group by khid having khid=a.khid and max(id)=a.id) and gypz=2

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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