8

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)

5
  • provide sample example Commented Dec 21, 2015 at 6:48
  • 5
    If you just want to update Col3 you can just leave off the other cols, e.g. Update TableTest SET Col3 = newdata Where Col1= D11 Commented Dec 21, 2015 at 6:50
  • you can use .... SET Col2 = CASE WHEN Col2 = '' THEN 'newdata' ELSE Col2 END, ...... this assumes meaning of empty is '' if null is empty or also expected you may need to modify the WHEN accordingly Commented Dec 21, 2015 at 7:08
  • " 56 rows and also also with thousands of records " So, does it have 56 or thousands? Commented Dec 21, 2015 at 8:44
  • 56 columns , it was my typing mistake Commented Dec 21, 2015 at 8:55

5 Answers 5

11

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!

Sign up to request clarification or add additional context in comments.

4 Comments

i only know the one way that i have share in my answer this is good.
UPDATE fetcheddata a INNER JOIN fetcheddata b ON a.OD_Order_ID_V = b.OD_Order_ID_V SET a.OD_Online_Listing_ID_V = 12 WHERE a.OD_Online_Listing_ID_V IS NULL and a.OD_Order_ID_V='CS44189505'; it doesnot helped me well
Check if it is NULL or blank value.
UPDATE fetcheddata a INNER JOIN fetcheddata b ON a.OD_Order_ID_V = b.OD_Order_ID_V SET a.EM_Date_D = '2015-12-20' WHERE (a.EM_Date_D IS NULL or a.EM_Date_D='') and a.OD_Order_ID_V='CS44189505'; dude working great , i owe to thankfull to you.
6

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

Comments

4

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

1 Comment

Can u explain what it will do ?
3

Just do it, like this:

Update TableTest SET  COL3 = newdata Where Col1= D11

1 Comment

It's too short without a minimum of explanation. Please, add more comments on your answer to make it a complete one.
2

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.