{"id":19,"date":"2008-03-21T11:12:56","date_gmt":"2008-03-21T16:12:56","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/03\/21\/insert-where-not-exists\/"},"modified":"2008-03-21T11:18:39","modified_gmt":"2008-03-21T16:18:39","slug":"insert-where-not-exists","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/03\/21\/insert-where-not-exists\/","title":{"rendered":"Insert where not exists"},"content":{"rendered":"<p>If you want to insert values into a MySQL table, but only if those values don&#8217;t already exist, and you don&#8217;t want to use a primary key on the table (and deal with the resulting error suppression ), here&#8217;s an elegant one-query method of doing so.<\/p>\n<pre>\r\nINSERT INTO [table name] SELECT '[value1]', '[value2]' FROM DUAL\r\nWHERE NOT EXISTS(\r\n    SELECT [column1] FROM [same table name]\r\n    WHERE [column1]='[value1]'\r\n    AND [column2]='[value2]' LIMIT 1\r\n)<\/pre>\n<p>OK, technically it&#8217;s not a single query. But it is only one round trip to the MySQL server.<\/p>\n<p>Hat tip to <a href=\"http:\/\/www.halogenandtoast.com\/\">Matt Mongeau<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to insert values into a MySQL table, but only if those values don&#8217;t already exist, and you don&#8217;t want to use a primary key on the table (and deal with the resulting error suppression ), here&#8217;s an elegant one-query method of doing so. INSERT INTO [table name] SELECT &#8216;[value1]&#8217;, &#8216;[value2]&#8217; FROM DUAL &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/03\/21\/insert-where-not-exists\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Insert where not exists&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[26],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/19"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}