开发者

Combining these three queries in one line in Postgres

开发者 https://www.devze.com 2023-03-23 16:31 出处:网络
I have these Postgres SQL, that is in between some while loop: while (.......) { ... $runStatement = \'update products set abc = \"\'.data[0].\'\" where abc = \"\'.data[1].\'\";\';

I have these Postgres SQL, that is in between some while loop:

while (.......) {
...  
$runStatement = 'update products set abc = "'.data[0].'" where abc = "'.data[1].'";';
...
}

while (.......) {
...  
$runStatement = 'update products set xyz = "'.data[0].'" where xyz = "'.dat开发者_如何学JAVAa[1].'";';
...
}

while (.......) {
...  
$runStatement = 'update extra_products set abc = "'.data[0].'" where abc = "'.data[1].'";';
...
}

where each loop, data is actually read from the same array.

Thanks.


Concatenate the statements with semicolons between them and run them in one call to the DB.


You don't need loops. You can do a JOIN in your update, the first table being for instance products, and the second table is a long list of VALUES (old,new),(old,new), etc. This allows to update many rows in one command and is much faster.

0

精彩评论

暂无评论...
验证码 换一张
取 消