Sunday, 16 September 2012

chk max(id) & insert in single query

In MYSQL, we need to query for finding max(id) & value is stored  in a variable, we need to use that variable in the query or for stored procedure.

but, we can use these both in a single query.

create table ty(i int,u varchar(20));
insert into ty(i,u) select (select ifnull(max(i),0)+1 from ty),'A';
insert into ty(i,u) select (select ifnull(max(i),0)+1 from ty),'B';
select * from ty;
drop table ty;

No comments:

Post a Comment