简体   繁体   English

如何同时执行多个查询更新?

[英]How to execute multiple query update at the same time?

So I have this three lines, and the problem is that only the last one is updating. 所以我有这三行,问题是只有最后一行正在更新。 What am I doing wrong? 我究竟做错了什么? What can I do to them to update all three at the same time? 我该怎么做才能同时更新所有三个?

$query = "UPDATE arak SET ara = '$konyha' WHERE ID = 1";
$query = "UPDATE arak SET ara = '$kugli' WHERE ID = 2";
$query = "UPDATE arak SET ara = '$ronk' WHERE ID = 3";

You should execute each single query otherwise you execute only the last query assigned at $query 您应该执行每个查询,否则仅执行在$ query处分配的最后一个查询

$query = "UPDATE arak SET ara = '$konyha' WHERE ID = 1"; 
your_execute_command() ... ;
$query = "UPDATE arak SET ara = '$kugli' WHERE ID = 2";
your_execute_command() ... ;
$query = "UPDATE arak SET ara = '$ronk' WHERE ID = 3";
your_execute_command() ... ;

or could yuo ca use a single query with a case clause 还是可以使用带有case子句的单个查询

  UPDATE arak
  SET ara = case when ID = 1 then '$konyha' 
                 when ID = 2 then '$kugli'
                 when ID = 3 then '$ronk' 
            else ara
            end 

只需将这些查询组合在一个由终止符分隔的字符串中,然后使用:

mysqli_multi_query($connection,$query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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