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;