MySQL INSERT INTO WHERE NOT EXIST

后端 未结 2 1946
孤街浪徒
孤街浪徒 2021-01-17 02:30

I will like to insert into same table if the ip_address for the product_id wasn\'t exist but preferably not to create two separate query. How can I achieve it?

Below

2条回答
  •  有刺的猬
    2021-01-17 03:02

    You can test the solution in the SQL-Fiddle.

    Below is the sample Insert statement.

    insert into temp
    (select 2,101,'2.2.2.2'
    from dual
    where not exists (select * from Temp t 
                  where t.product_id = 101 and
                        t.ip_address = '2.2.2.2'));
    

提交回复
热议问题