设为首页 - 加入收藏 华夏网 (http://www.hxwgxz.com)- 云主机,资讯,互联网,人工智能,云计算,大数据,区块链,VR,站长网!
热搜: 2019 google 中国 2020
当前位置: 主页 > 编程 > 正文

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

发布时间:2021-02-27 05:44 所属栏目:[编程] 来源:网络整理
导读:如何使用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

如何使用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列的分配.

【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

网友评论
推荐文章