Php mysql updates only empty column

I have 4 columns in a table and I have data for 3 columns as below

TableTest Col1 | Col2 | Col3 D11 | D12 | D21 | D22 | 

Usually the update request will be

 Update TableTest SET Col1 = D11 , Col2 = D12 , COL3 = newdata Where Col1= D11 

Scenario: an update request should only transfer data to COL3, it should skip Col1 and Col2 because it is already filled with data (even if the same or different data for Col1 and Col2)

+8
sql php mysql sql-update
source share
5 answers

This can help -

 UPDATE TableTest a INNER JOIN TableTest b ON a.Col1 = b.Col1 SET a.Col3 = 'newData' WHERE a.Col3 IS NULL 

An INNER JOIN with the same table so that it updates the corresponding row!

+11
source share

If you just want to update COL3 and not include other columns in the UPDATE query.

Query:

 Update TableTest SET COL3 = newdata Where Col1= D11 
+6
source share

You should update the whole table using one query , like:

 Update TableSet SET COL3=CONCAT('D',CONVERT(Substr(Col2,2),INT)+1) 

This will update the table as follows:

 TableTest Col1 | Col2 | Col3 D11 | D12 | D13 D21 | D22 | D23 
+4
source share

Just do it like this:

 Update TableTest SET COL3 = newdata Where Col1= D11 
+3
source share

In an UPDATE query, there is no need to reassign the value of col1 and col2 if these values ​​are not changed.

 UPDATE TableTest SET COL3 = newdata WHERE Col1= 'D11'; 
+2
source share

All Articles