加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

php – 插入查询后更新状态

发布时间:2020-12-11 23:46:21 所属栏目:MySql教程 来源:网络整理
导读:$stmt = "insert into {CI}payment_logs (customer_id,invoice_id,invoice_total_amount,invoice_paid_amount,customer_paid_amount,invoice_payment_on ) " . "values( ?,?,NOW() )";$this-db-query( $stmt,array( $Customer_Id,$Invoice_Id,$Invoice_Total

$stmt = "insert into {CI}payment_logs (customer_id,invoice_id,invoice_total_amount,invoice_paid_amount,customer_paid_amount,invoice_payment_on ) "
                . "values( ?,?,NOW() )";
$this->db->query( $stmt,array( $Customer_Id,$Invoice_Id,$Invoice_Total_Amount,$Deducted_Amount,$Paid_By_Cust ) );        
$Log_Id = $this->db->insert_id(); // Returns Last Insert ID

根据我的需要,这是正常工作,但我必须在特定条件下更新同一个表中每一行的状态.看看图片,

这是上述查询的插入数据.正如您在invoice_total_amount中看到的那样是总金额,但是这个金额在第三行数据中完成.在高亮点数量SUM等于invoice_total_amount之后,我必须将invoice_status字段更新为已付款,并且在休息字段中我必须将状态更新为部分.我尝试更新状态以添加触发器但不成功.

然后我尝试使用此命令的PHP.

update {CI}payment_logs 
    set invoice_status = IF( invoice_total_amount = sum(invoices_paid_amount),'paid','partial' ) 
where log_id = ?

但我没有得到invoices_paid_amount的SUM,其中invoice_id = 5,我尝试在IF条件下使用子查询,但它无效我的查询.

请帮我解决单查询或触发解决方案..

最佳答案 加入计算总和的子查询.

UPDATE {CI}payment_logs l1
JOIN (SELECT invoice_id,SUM(invoices_paid_amount) total
      FROM {CI}payment_logs
      GROUP BY invoice_id) l2
ON l1.invoice_id = l2.invoice_id
SET invoice_status = IF(invoice_total_amount = total,'partial')
WHERE log_id = ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读