SQL: update field value to itself Feb20 '08
Sometimes your application contains query update code that you don't wish to modify, or that you don't have access to modify.
For example, using PHP:
UPDATE table SET field1 = " . $value1 . ", field2 = " . $value2 . " WHERE id = " . $id . ";
You may still have access to modify the values that get used in the query - in this case, $value1 and $value2.
Sometimes you don't want to update a particular field with a new value - you'd like to keep it as it is. However, despite whether or not a value should be something new, the UPDATE query still runs, so you need some way of telling the query to leave a particular field value as is.
The easiest way to avoid updating a field is to simply exclude it from the query:
UPDATE table SET field2 = " . $value2 . " WHERE id = " . $id . ";
In this case, we removed the instructions to update field1. If we don't tell it to update field1, it leaves the value as is.
But remember - we don't want to modify the query instructions, or we may not have access to modify it. The instructions run regardless of fields that should or should not get updated.
For the field that needs to remain the same, you could set it's value to itself. Our modified query would look like:
UPDATE table SET field1 = field1, field2 = " . $value2 . " WHERE id = " . $id . ";
Notice field1 is set to itself:
field1 = field1
No need to adjust the query instructions. The field value remains the same.
Add Feedback (view all)
Leave feedback
- Commonly misused phrases and clichés
- Bluetooth headset tips and ideas
- Customize Brightkite-to-Twitter updates
Popular Pages
- Fast rounded corners in Photoshop (7424 recent visits)
- PHP – passing variables across pages (2558 recent visits)
- JavaScript set selected on load (2413 recent visits)
- Removing all child nodes from an element (1828 recent visits)
- Firefox 3 smart address bar: wildcard search (1755 recent visits)
- iPod songs out of order? (1314 recent visits)
- Britney - Everytime piano tab (1138 recent visits)
- MySQL LEFT JOIN syntax (884 recent visits)
- Firefox 3 smart address bar: wildcard search (722 recent visits)
- Date difference in MySQL (651 recent visits)
Similar Entries
- Full date to SQL date within Excel (54 recent visits)
- CD swapping: update on my Lala experience (2 recent visits)
- Google Docs appearance update (1 recent visits)
- Google Video refund update (3 recent visits)
- Another update on my lack of Leopard (8 recent visits)
- Please update your firmware (0 recent visits)
Stats
156 unique visits since July 2008
Recent Referrers (click)
- update
- sql where field is different
- +sql +update +set +time +value
- update date field with sql update query
- sql can we update with a field with another value from another field
- change field values javascript
- sql update table from itself
- access sql change field with query
- sql syntax to update field
- sql update field value query
- update a field with its rounded value in sql
- sql update field1 field2
- mysql add field to itself
- update field value with value from field in different table
- sql query update change field
- sql update another field value
- php update field sql
- sql update same value
- php update field
- php + update+ field
Or you can leave field1 out of the query and it won't be touched. ... Read more.