0

Hi I have a SQL table as below;

--------------------------
col1 | col2 | col3 | col4|
--------------------------
 10  |  20  | 30   | 40  |
 15  |  22  | 12   | 21  |
 11  |  40  | 50   | 60  |

and I'm trying to delete column specific data from the table. For instance I want to delete 50 and 60 in respective 3rd and 4th columns.

I use the following code but it deletes the entire row which contains 11 and 40 as well. Code is as follows. can some one tell me how to alter my approach below? Thanks.

 $result = mysqli_query($con,"DELETE FROM table1 WHERE col1 ='11'");
1
  • What is the condition that determines the row which includes data to be deleted? How do you know which columns will be deleted? Commented Oct 23, 2012 at 15:55

2 Answers 2

3

Use the UPDATE commands to set the column to null or '' (empty). The DELETE deletes an entire row.

$result = mysqli_query("UPDATE `table1` SET `col3`=NULL WHERE `col1`='11';");
Sign up to request clarification or add additional context in comments.

Comments

2

You need to update the row, instead of delete, like this,

$result = mysqli_query($con,"UPDATE table1 SET col3='', col4='' WHERE col1 ='11'");

NOTE: Update is used for changing the value in specific columns, keeping the row intact. Delete is for deleting/removing the entire row, from the table.

2 Comments

If I use 'Update' and at a different time I want to delete 11 and 40 also which is in the same row, will the 3rd row still be there in the table as a blank row?, which possibly consume some capacity of the database?
If you want to delete/remove/change a value in a specific column, you should use update. If at a different time, you want to remove the entire row (make all columns empty), then you should use delete to delete the entire row.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.