{"id":266,"date":"2011-06-10T11:58:31","date_gmt":"2011-06-10T11:58:31","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=266"},"modified":"2011-06-10T11:58:31","modified_gmt":"2011-06-10T11:58:31","slug":"sql-conditional-update-if-null","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/sql-conditional-update-if-null\/","title":{"rendered":"SQL Conditional Update &#8211; Update only if NULL"},"content":{"rendered":"<p>When you find yourself needing to update a record column only if the column is currently NULL (where you don&#39;t want to overwrite actual data), you might be tempted to use the three-steps below:<\/p>\n<ol>\n<li><code>SELECT thecolumn FROM thetable WHERE id = xxx<br \/>\n\t\t<\/code><\/li>\n<li>If thecolumn is empty, then<\/li>\n<li><code>UPDATE thetable SET thecolumn = &#39;value&#39;<br \/>\n\t\t<\/code><\/li>\n<\/ol>\n<p>This not only requires two trips to your database, but also adds unneeded logic and lines of code to your script.<\/p>\n<p>The update statement below takes care of the three steps above by including a conditional:<\/p>\n<p><code>UPDATE table<br \/>\n\tSET thecolumn = IF(thecolumn IS NULL, &#39;value&#39;, thecolumn)<br \/>\n\tWHERE id = xxx<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you find yourself needing to update a record column only if the column is currently NULL (where you don&#39;t want to overwrite actual data),&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[41,13],"tags":[95],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/266"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/266\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}