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;