user3785340
user3785340

Reputation: 25

Insert rows from one table to other table dynamically

I have two tables in my database

create table A1 (username varchar(50), Sn int);
create table A2 (username varchar(50), Sn int);

i am inserting the data into table A2

insert into A2 values("abhi",1);
insert into A2 values("abhi",2);
insert into A2 values("abhi",3);
insert into A2 values("abhi",4);
insert into A2 values("abhi",5);
insert into A2 values("abhi",,6);

After that i have to insert the data from table A2 to A1 in such a way that at the first time row insertion will be from 1 to 3 and at the second time row insertion will be from 4 to 6

Please help me i am new to Mysql

Upvotes: 0

Views: 79

Answers (2)

user3785340
user3785340

Reputation: 25

I have some doubt will this query help if i will have 1 lakh records in the table. I think while checking max it will slow down the performance . I am using event scheduler for this which will run this query automatically after every 5 minutes

Upvotes: 0

Sathish
Sathish

Reputation: 4487

Try Like This

insert into A1 select * from A2 where Sn >= 
(select (case when max(Sn) is null then 0 else max(Sn) end) +1 from A1) 
Limit 3;

Upvotes: 1

Related Questions