php-更新查询MySQL PDO时的情况

前端之家收集整理的这篇文章主要介绍了php-更新查询MySQL PDO时的情况 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如何使用CASE WHEN在MySQL和PDO中运行更新查询.
以下查询将CASE列更新为0.

$query= "Update payments SET 
                            total = :total,paid = CASE
                                      WHEN paid > :new THEN paid = :new
                                   ELSE paid
                                   END,due = CASE
                                     WHEN paid < :new THEN due = :new - paid
                                  ELSE due
                                  END
                             WHERE id = :id ";

                        $update= $db->prepare($query);

                        $update->bindparam(':total',$new);
                        $update->bindparam(':new',$new);
                        $update->bindparam(':id',$id);
                        $update->execute();
最佳答案
请勿尝试将值分配给CASE WHEN语句内的列,因为您已经在这样做了.
CASE WHEN将评估为满足条件的值.
试试这个代码

UPDATE payments SET 
 total = :total,paid = (CASE WHEN paid > :new THEN :new ELSE paid END),due = (CASE WHEN paid < :new THEN (:new - paid) ELSE due END)
 WHERE id = :id 

删除了case语句中对pay和due列的分配.

猜你在找的MySQL相关文章