Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update MySQL without specifying column names

Tags:

mysql

I want to update a mysql row, but I do not want to specify all the column names. The table has 9 rows and I always want to update the last 7 rows in the right order. These are the Fields

id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant

Is there any way I can update the last few records without specifying their names? With an INSERT statement this can be done pretty easily by doing this:

INSERT INTO `settings`
VALUES (NULL, ...field values...)

So I was hoping I could do something like this:

UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...

But unfortunately that doesn't work.


1 Answers

If the two first columns make up the primary key (or a unique index) you could use replace

So basically instead of writing

UPDATE settings
   SET fangate = $fangate,
       home = $home,
       thanks = $thanks
       overview = $overview,
       winner = $winner,
       modules.wallPost = $modules.wallPost,
       modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId

You will write

REPLACE INTO settings
 VALUES ($id, 
         $projectId,
         $fangate,
         $home,
         $thanks
         $overview,
         $winner,
         $modules.wallPost,
         $modules.overviewParticipant)

Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.

like image 159
Andreas Wederbrand Avatar answered Mar 10 '26 13:03

Andreas Wederbrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!