Home > Archive > MySQL Server Forum > August 2005 > Conditional insert?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Conditional insert?
hoonew

2005-08-18, 8:23 pm

I need to do in SQL the following psuedo-code, and I'm stuck:

IF ((COUNT(*) FROM a WHERE column_a=1)>0)
THEN
INSERT INTO TABLE b
ENDIF
Bill Karwin

2005-08-18, 8:23 pm

hoonew wrote:
> I need to do in SQL the following psuedo-code, and I'm stuck:
>
> IF ((COUNT(*) FROM a WHERE column_a=1)>0)
> THEN
> INSERT INTO TABLE b
> ENDIF


create table a (column_a integer);
create table b (foo varchar(10));
insert into a (column_a) values (1), (1);

insert into b (foo)
select distinct 'foo' as foo
from a where column_a = 1;
....one record inserted, because of distinct.

insert into b (foo)
select distinct 'foo' as foo
from a where column_a = 2;
....zero records inserted, because no records in `a` match.

Bill
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com