Random thoughts & observations

From the mundane to the profound and everything in between here’s what’s rocking our world

Updating a range of records with MySQL

Posted: January 8, 2016
Written by: Saints At Play
Category: Server

Updating a single record entry in a MySQL database table is fairly simple, but what if you need to update a batch of records with one single query?

It's not as difficult as you might think...

Using your table's primary key as the field for determining the ranges to update within/outside of you can write a query similar to the following (substituting the table/field names and range values listed below with your own):

UPDATE `business_details` SET `business_area` = "London/Greater London", `opening_hours` = "Mon - Sat (10AM - 6PM)" WHERE `location_id` BETWEEN 35 AND 50;

If you're using a database management system such as phpMyAdmin you can simply use the above query in the SQL tab (again paying attention to replace the table/field names and values with your own - and don't forgot to use the backtick character for 'escaping' those table/field names).

Alternatively the above can be modified to be used in a PHP (or similar) script and directly from the MySQL command line itself (if your hosting account provides such a facility).

Nice and easy, huh?

« Return to Posts

Comments

There are no comments

Posting comments after three months has been disabled.