INSERT if the value doesn't already exists
conditional-statements, insert, mysql
Solution
INSERT INTO tickets_users (tickets_id, users_id, type)
select 455, 1444, 2 from dual
WHERE not exists (select 1 from tickets_users where type = 2 and tickets_id = something)
Try this
Edit : To add multiple rows
INSERT INTO tickets_users (tickets_id, users_id, type)
select 455, 1444, 2 from dual
WHERE not exists (select 1 from tickets_users where type = 2 and tickets_id = 455)
union all
select 456, 1444, 2 from dual
WHERE not exists (select 1 from tickets_users where type = 2 and tickets_id = 456)
Problem
In my database, I have `tickets_users` that affiliate users to a ticket. I'd like to do an `INSERT` that checks in `tickets_users` if there is not already this `users_id` (for example user 1444) and `type = 2 WHERE tickets_id = something` (for example ticket number 455); if it already exists do nothing, else ``` INSERT INTO tickets_users (tickets_id, users_id, type) VALUES (455, 1444, 2) WHERE tickets_id = 455 ```