Search code examples
phpmysqlsql-updatebulk

MySQL bulk query update in a single query using multiple criterias


This looks like a common question among many but I am yet to find a solution that helps.

I have been able to find a bulk insert query that requires only a single query but for update, the structure of the query is a fair bit different.

Is it possible to create an update query that works with many different lines.

I am already able to create a query that runs the correct number of times and updates the rows correctly but it runs multiple times.

This creates quite a stack of queries which as everyone knows, can take alot of processing.

Here is the bulk insert query I have found:

$sql = array();  
foreach( $data as $row ) {     
    $sql[] = '("'.mysql_real_escape_string($row['text']).'", '.$row['category_id'].')'; 
    } 
mysql_query('INSERT INTO table (text, category) VALUES '.implode(',', $sql)); 

Solution

  • You can't do this with an update query. You'll have to make one query per update.

    Unless ofcourse you want to update based on a field like: UPDATE TABLE SET field_1=1 WHERE field_2=2