05-13-08, 10:11 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
UPDATE mysql
is there a way to update multiple columns in one query? I can't seem to find the syntax as most tutorials are based around a single change
trying to change this insert query into an update query
PHP Code:
$query = "INSERT INTO main_inventory (
item_type, title, brand, model, serial_no, replacement_cost, quantity, kit_content, main_location ,current_location, notes
) VALUES (
' { $item_type } ',' { $title } ', ' { $brand } ',' { $model } ',' { $serial_no } ', { $replacement_cost } , { $quantity } , ' { $kit_content } ',' { $main_location } ',' { $current_location } ',' { $notes } ')" ;
05-13-08, 10:34 AM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
it follows the syntax
SQL Code:
UPDATE `tablename` SET
`column1` = 'value1' ,
`column2` = 'value2'
WHERE
`idcolumn` = 'something'
You need the idcolumn or some other column to verify it's the right one that you are updating. If you are wanting to update all columns, you simply remove the everything after and including the WHERE
05-14-08, 03:23 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Jay6390
If you are wanting to update all columns, you simply remove the everything after and including the WHERE
sorry, I don't quite follow. Are you saying there is a quicker way than listing ALL the columns in series
05-14-08, 03:50 AM
Wannabe Coder
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
As far as I know, there is no such way in case when you wish to update specific columns with specific data.
Last edited by Wolf1994; 05-14-08 at 03:52 AM .
05-14-08, 06:22 AM
Newbie Coder
Join Date: Mar 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
sorry, think I misinterpreted that. You mean to update all records, not all columns
05-14-08, 06:28 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Moving this to Database section...
05-14-08, 06:38 AM
Wannabe Coder
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, I misinterpreted too. I meant records too.
If you are searching a quicker update and this is total update - i.e. update of all columns in record, you may delete all records with DELETE query and after that insert them anew with one INSERT query.
05-14-08, 08:29 AM
Wannabe Coder
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Rereaded the original post...
You may update several columns at once with such syntax:
SQL Code:
UPDATE `your_table` SET `column1` ='value1' ,`column2` ='value2' WHERE `id` ='xxx'
Last edited by UnrealEd; 05-14-08 at 08:32 AM .
Reason: please use the [highlight=SQL] wrapper when posting SQL code
05-14-08, 08:32 AM
Wannabe Coder
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off