如何为MySQL生成此序列?
(N.B。: - 对于每个唯一对,重新生成序列)
1,1 - 1
1,1 - 2
1,1 - 3
1,2 - 1
1,2 - 2
1,3 - 1
1,4 - 1
1,4 - 2
1,4 - 3
答案 0 :(得分:1)
创建表:-
create table dummy(id varchar(100));
在表中插入值:-
insert into dummy (id) values('1,1');
insert into dummy (id) values('1,1');
insert into dummy (id) values('1,1');
insert into dummy (id) values('1,2');
insert into dummy (id) values('1,2');
insert into dummy (id) values('1,3');
insert into dummy (id) values('1,4');
insert into dummy (id) values('1,4');
insert into dummy (id) values('1,4');
运行以下查询:-
select id,@aaaa:=ifnull(seq,@aaaa:=@aaaa+1)as seq from(select y.id,b,seq from (select
id,@aaa:=@aaa+1 as b from dummy cross join (select @aaa:=0)a) as y left join
(select id,a,@aa:=@aa as seq from (select * from (select id,@a:=@a+1 as a from dummy
cross join (select @a:=0)a)as b group by id)a cross join (select @aa:=1)as d )
as x on x.a=y.b) as z cross join (select @aaaa:=0)a;
答案 1 :(得分:0)