Home > Net >  If I launch this insert twice
If I launch this insert twice

Time:05-11

If I launch this insert twice, it adds duplicate products to me. how do I add if something has changed?

INSERT INTO db1 ("Drink") VALUES
    ('Cola'),
    ('Rom'),
    ('Sprite'),
    ('Fanta'),
    ('Vodka');

CodePudding user response:

INSERT INTO db1 ("Drink")
select
drinks.drink
from
(
VALUES
    ('Cola'),
    ('Rom'),
    ('Sprite'),
    ('Fanta'),
    ('Vodka')
)
as drinks(drink)
where not exists (select 1 from db1 where db1.drink=drinks.drink)
  •  Tags:  
  • sql
  • Related