sql - INSERT INTO only if not exists MYSQL -


i'm insert data way of using select , not values default. problem can't find way insert data if it's not in database.

this current query:

insert collectives_users(id_user,id_artistname,id_collective,users_type,status) select     (select id_user artistnames artistname = 'yoannis'),     (select id artistnames artistname = 'yoannis'),     ('1'),(2),(0) union select     (select id_user artistnames artistname = 'paul'),     (select id artistnames artistname = 'paul'),     ('1'),(4),(0) 

i tried with

insert ignore 

doesn't work , not appropriate (not returning error)

i tried with

and not exist 

doesn't work.

if have idea don't hesitate.

have tried replace syntax? works same insert, use replace instead of insert.

how setting dummy column in collectives_users table, example count times duplicate found. way, use insert ... on duplicate key update syntax:

insert collectives_users(id_user,id_artistname,id_collective,users_type,status) select     (select id_user artistnames artistname = 'yoannis'),     (select id artistnames artistname = 'yoannis'),     ('1'),(2),(0) union select     (select id_user artistnames artistname = 'paul'),     (select id artistnames artistname = 'paul'),     ('1'),(4),(0) on duplicate key update dummy_column = dummy_column + 1; 

another way create stored procedure. more tricky.

sample code:

delimiter \\ drop procedure if exists sp_test\\ create procedure sp_test(in_id_user, in_id_artistname, in_id_collective, in_users_type, in_status) begin  case     when not exists (select id_user artistnames artistname = 'yoannis')  or ...     insert ... end case;  end\\ delimiter ; 

more info @ mysql reference manual insert ... on duplicate key update , case syntax1.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -