Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update an entire row in MySQL

Tags:

php

mysql

I'm new to MySQL and I want to update an entire row in MySQL with a new array, so far all the examples of the Update query involves specifying the column name and a new value for that column, like this:

"UPDATE tablename SET columnname = '".$new_value."' WHERE columnname = '".$value."'";

How can I update an entire record with the update query or should I use the replace query?

Any advice will be appreciated.

Edit: Is there a query which doesn't require to specify all the column names and new column values?

Basically I want to have a query that looks something like this:

Update entirerow with thisarray where primarykeycolumn='thisvalue'

like image 283
grasshopper Avatar asked May 13 '12 22:05

grasshopper


2 Answers

That is the correct way.

UPDATE TABLENAME SET COLUMNAME = VALUE, COLUMN2NAME = VALUE, ETC WHERE CONDITION
like image 62
LJ Wilson Avatar answered Oct 01 '22 09:10

LJ Wilson


To do that you need to

  1. Enumerate all the values
  2. Know the primary key column and value

So the final query would look like

UPDATE tablename
   SET col1 = 'val1', col2 = 'val2' ...
 WHERE id = id_value

There is no any magic command for updating the "whole row" in sql other than I shown above. And REPLACE is definitely not what you need here.

like image 25
zerkms Avatar answered Oct 01 '22 09:10

zerkms