简体   繁体   中英

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)); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM