这样的sql 语句怎么写?

MySql 码拜 9年前 (2015-11-15) 873次浏览
订单号 	会员ID	交易金额	交易时间
1		A		12.0		1440483785692
2		A		11.0		1410483785692
3		A		11.0		1420483785692
4		B		22.0		1440483785692
5		B		32.0		1430483785392
6		B		12.0		1420483785692
7		C		12.0		1440483785292
8		C		42.0		1440483785692
9		C		52.0		1420483785622

本人想查询出表中每个会员最近的订单情况,怎么查?

解决方案:50分
试试这个:

select tb.订单号,tb.会员ID.tb.交易金额,tb.交易时间
from tb
inner join
(
select 会员ID,max(交易时间) 交易时间
from tb
group by 会员ID
)t
on tb.会员ID = t.会员ID and tb.交易时间 = t.交易时间
order by tb.会员ID,tb.订单号
解决方案:20分
参考下贴中的多种方法
http://blog.csdn.net/acmain_chm/article/details/4126306
[征集]分组取最大N条记录方法征集,及散分….
解决方案:20分
select * from  table a
where 1 >  (select count(*) from table where 会员ID = a.会员ID and  交易时间> a. 交易时间)
解决方案:10分
select * from tt a where not exists(
select 1 from tt where a. 会员ID = 会员ID    and  a.  交易时间<交易时间
)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明这样的sql 语句怎么写?
喜欢 (0)
[1034331897@qq.com]
分享 (0)