Answer the question
In order to leave comments, you need to log in
Insert if not exist MySQL?
mysqli_query($connect, "INSERT INTO wishlist (user_id, product_id) VALUES (12, 3) WHERE NOT EXISTS (SELECT id FROM wishlist WHERE user_id = 12 AND product_id = 3)");
Answer the question
In order to leave comments, you need to log in
insert with WHERE do not seem to coexist. You can either use if or make this key pair unique and use insert on duplicate key ignore.
insert into wishlist(user_id, product_id)
select t.user_id, t.product_id
from (
select 12 as user_id, 3 as product_id
) t
left outer join wishlist w on t.product_id = w.product_id and t.user_id = w.user_id
where w.user_id is null and w.product_id is null
start transaction;
create temporary table tempWishlist as select * from wishlist where 0 = 1;
# temporary table load:
# insert into tempWishlist(...) values(...)
insert into wishlist(user_id, product_id)
select t.user_id, t.product_id
from tempWishlist t
left outer join wishlist w on t.product_id = w.product_id and t.user_id = w.user_id
where w.user_id is null and w.product_id is null
commit;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question