user5621153
user5621153

Reputation:

Php mysql to update only empty column

Consider I have 4 columns in a table and i have datas for 3 columns like below

TableTest

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

Normally the update query would be

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

The Scenario is , the update query should only push data to the COL3 , it should skip the Col1 and Col2, as it has already filled with data(even if same or different data for the Col1 and Col2)

Upvotes: 8

Views: 2315

Answers (5)

devpro
devpro

Reputation: 16117

If you just want to update COL3 than don't include other columns in UPDATE query.

Query:

Update TableTest SET COL3 = newdata Where Col1= D11

Upvotes: 6

Muhammad Muazzam
Muhammad Muazzam

Reputation: 2800

You should update whole table using single query as:

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

This will update table as follows:

TableTest

Col1  | Col2  | Col3
D11   | D12   |  D13 
D21   | D22   |  D23

Upvotes: 4

Sougata Bose
Sougata Bose

Reputation: 31749

This might 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 appropriate row!

Upvotes: 11

Saharsh Shah
Saharsh Shah

Reputation: 29051

In UPDATE query, there is no need to reassign the value of col1 and col2 if those values are not changing.

UPDATE TableTest 
SET COL3 = newdata 
WHERE Col1= 'D11';

Upvotes: 2

Felix Kamote
Felix Kamote

Reputation: 342

Just do it, like this:

Update TableTest SET  COL3 = newdata Where Col1= D11

Upvotes: 3

Related Questions