mysql 类似rownum改写功能
create table `sq` ( `id` int , `user_id` varchar (45), `userid_seq` varchar (45) ); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘1‘,‘zhang‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘2‘,‘wang‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘3‘,‘han‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘4‘,‘zhang‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘5‘,‘zhang‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘6‘,‘wang‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘7‘,‘han‘,NULL); insert into `sq` (`id`, `user_id`, `userid_seq`) values(‘8‘,‘zhang‘,NULL);
实现相同user_id产生一个类似rownum序号功能
实现语句
select a.*,(select count(*) from sq b where b.user_id=a.user_id and b.id <= a.id ) as seqno from sq a;
某个id最大的序列号 也就是有多少条
select max(seqno) from (select a.*,(select count(*) from sq b where b.user_id=a.user_id and b.id <= a.id ) as seqno from sq a where user_id=‘zhang‘) as c;
本文出自 “深山有dba” 博客,请务必保留此出处http://19801021.blog.51cto.com/7262596/1585984
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。