title: Only Update a certain column in MySQL Query if it has a certain value set
body: <p>I have a Project Management application I am building with PHP and MySQL and the function below is used to mass update all Tasks associated with a project, in some cases this could be 200-300 MySQL records being updated at once.</p>
  
  <p>I just realized there is a flaw in my design here though.  This function UPDATES my database columns <code>status</code>, <code>date_modified</code>, and <code>date_completed</code></p>
  
  <p>This is great most of the time but I just realized that often a user will forget to "start" a Task which sets the <code>date_started</code> column.</p>
  
  <p>If the <code>date_started</code> column is never set, it will have a value of <code>0000-00-00 00:00:00</code> and with a <code>date_started</code> column set to <code>0000-00-00 00:00:00</code> and the <code>date_completed</code> column set to NOW...this breaks my Gantt chart functionality for these Tasks.</p>
  
  <p>So I need to modify my SQL below so that when setting a <code>status</code>  to <code>completed</code> it will somehow check to see if the <code>date_stared</code> column === <code>0000-00-00 00:00:00</code> and if it does, it will need to also update that column to NOW.</p>
  
  <p>I believe this is possible using <code>CASE</code> in MySQL instead of having to query each and every record into PHP to do the check and then do a separate UPDATE when needed but my experience is very little in these more advanced SQL features.</p>
  
  <p>Could someone help me with a solution?</p>
  
  <pre><code>public function completeAllTasks($projectId, $completed_date = true){
      // Update Project Task STATUS and Date_MODIFIED
      $sql = "UPDATE  `project_tasks`
      SET  `status` =  'Completed',
              `modified_user_id` = '$this-&gt;user_id',
              `date_completed` = UTC_TIMESTAMP()
              `date_modified` = UTC_TIMESTAMP()
      WHERE  `project_id` = '$projectId'
      AND `status` != 'Completed'
      AND `heading` != '1'";
  
      return $this-&gt;db-&gt;query($sql);
  }
  </code></pre>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>I did some testing and this seems to do the trick...</p>
  
  <pre><code>`date_started` = (CASE
                      WHEN date_started = '0000-00-00 00:00:00'
                        THEN UTC_TIMESTAMP()
                        ELSE date_started
                  END)
  </code></pre>