Only Update a certain column in MySQL Query if it has a certain value set

JasonDavis

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.

I just realized there is a flaw in my design here though. This function UPDATES my database columns status, date_modified, and date_completed

This is great most of the time but I just realized that often a user will forget to "start" a Task which sets the date_started column.

If the date_started column is never set, it will have a value of 0000-00-00 00:00:00 and with a date_started column set to 0000-00-00 00:00:00 and the date_completed column set to NOW...this breaks my Gantt chart functionality for these Tasks.

So I need to modify my SQL below so that when setting a status to completed it will somehow check to see if the date_stared column === 0000-00-00 00:00:00 and if it does, it will need to also update that column to NOW.

I believe this is possible using CASE 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.

Could someone help me with a solution?

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->user_id',
            `date_completed` = UTC_TIMESTAMP()
            `date_modified` = UTC_TIMESTAMP()
    WHERE  `project_id` = '$projectId'
    AND `status` != 'Completed'
    AND `heading` != '1'";

    return $this->db->query($sql);
}

UPDATE

I did some testing and this seems to do the trick...

`date_started` = (CASE
                    WHEN date_started = '0000-00-00 00:00:00'
                      THEN UTC_TIMESTAMP()
                      ELSE date_started
                END)
Brian
$sql = "UPDATE  `project_tasks`
SET  `status` =  'Completed',
      `modified_user_id` = '$this->user_id',
      `date_completed` = UTC_TIMESTAMP(),
      `date_modified` = UTC_TIMESTAMP(),
      `date_started` = CASE
          WHEN date_started = '0000-00-00 00:00:00' THEN NOW()
          ELSE date_started
       END
WHERE  `project_id` = '$projectId'
AND `status` != 'Completed'
AND `heading` != '1'";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I "Delete All" but only when a column has a certain value?

From Dev

how to update column that has (NULL) value to 'A' in mysql

From Dev

Return Certain Value Only With Certain Parameters

From Dev

MySQL query filter only rows containing certain value, after join

From Dev

MYSQL, update only certain rows of a table which satisfy certain conditons

From Dev

Update column with certain value to a column value with same id

From Dev

Bind a data frame only if a value on a certain column is not a duplicate

From Dev

Mysql query get data if certain column is on sequence

From Dev

Only proceed to read XML if variable has certain value

From Dev

mysql query apply condition to where clause only if certain condition satisfied

From Dev

Disable button if a specific column has a certain value

From Dev

Only selecting values if other column has a certain value in BigQuery

From Dev

Row count in Hbase where a column has certain value

From Dev

Update certain column of recordset from textbox value

From Dev

CREATE TABLE with column of certain value if other column has a value

From Dev

SQL Query: Get rows where value of certain column is maximum

From Dev

Create View in MySQL of only rows with a certain string in column

From Dev

MySQL Triggers: How do I only insert if a certain column is populated?

From Dev

SQL server query: only certain column values into row, iterating

From Dev

update a certain column with a set value

From Dev

How to set MYSQL column to certain value if a certain row exists?

From Dev

Retrieve header of column if it has a certain value

From Dev

Select * but group by a certain column, only for a certain column

From Dev

SQL query exclude a certain value from same column same table?

From Dev

How to SUM by group where column has certain value

From Dev

Average a certain column value in MySQL

From Dev

Checking if a column has a certain value, then restrict another by a list

From Dev

how to replace a certain value of a column in MYSQL?

From Dev

Select only IDs where a certain column only contains a certain value

Related Related

  1. 1

    How can I "Delete All" but only when a column has a certain value?

  2. 2

    how to update column that has (NULL) value to 'A' in mysql

  3. 3

    Return Certain Value Only With Certain Parameters

  4. 4

    MySQL query filter only rows containing certain value, after join

  5. 5

    MYSQL, update only certain rows of a table which satisfy certain conditons

  6. 6

    Update column with certain value to a column value with same id

  7. 7

    Bind a data frame only if a value on a certain column is not a duplicate

  8. 8

    Mysql query get data if certain column is on sequence

  9. 9

    Only proceed to read XML if variable has certain value

  10. 10

    mysql query apply condition to where clause only if certain condition satisfied

  11. 11

    Disable button if a specific column has a certain value

  12. 12

    Only selecting values if other column has a certain value in BigQuery

  13. 13

    Row count in Hbase where a column has certain value

  14. 14

    Update certain column of recordset from textbox value

  15. 15

    CREATE TABLE with column of certain value if other column has a value

  16. 16

    SQL Query: Get rows where value of certain column is maximum

  17. 17

    Create View in MySQL of only rows with a certain string in column

  18. 18

    MySQL Triggers: How do I only insert if a certain column is populated?

  19. 19

    SQL server query: only certain column values into row, iterating

  20. 20

    update a certain column with a set value

  21. 21

    How to set MYSQL column to certain value if a certain row exists?

  22. 22

    Retrieve header of column if it has a certain value

  23. 23

    Select * but group by a certain column, only for a certain column

  24. 24

    SQL query exclude a certain value from same column same table?

  25. 25

    How to SUM by group where column has certain value

  26. 26

    Average a certain column value in MySQL

  27. 27

    Checking if a column has a certain value, then restrict another by a list

  28. 28

    how to replace a certain value of a column in MYSQL?

  29. 29

    Select only IDs where a certain column only contains a certain value

HotTag

Archive