如何根据条件更新记录?

时间:2016-08-01 09:49:56

标签: mysql sql

我在此表中有一个表tbl_loyaltypoints,列status = 2具有默认值

现在我有以下查询来获取所有记录

select lp.order_id, DATEDIFF(CURDATE(), FROM_UNIXTIME(lp.created_at)) as createon,
       oi.deal_id, wo.returnWithin, lp.status
from tbl_loyaltypoints lp
inner join tbl_orders ord on lp.order_id = ord.id
inner join tbl_order_item oi on lp.order_id = oi.order_id 
inner join tbl_workorders wo on oi.deal_id = wo.id 
where ord.order_type = 1
order by lp.id DESC;

输出:

order_id  createon   deal_id  returnWithin status 
1045        4           160     20          2
1044        4           160     20          2
1043        20          160     20          2

我想在tbl_loyaltypoints.status时更新状态createon==returnWithin

有没有办法用Mysql做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以将INSERT INTO leave_records (ID_NUM, TYPE, ADDITIONAL, DATE) SELECT tbl_leave.ID_NUM, 'VL/SL', '1.25', CURDATE() FROM tbl_leave WHERE tbl_leave.STATUS = 'On'转换为select

join

您没有指定新状态。 update tbl_loyaltypoints lp inner join tbl_orders ord on lp.order_id = ord.id inner join tbl_order_item oi on lp.order_id = oi.order_id inner join tbl_workorders wo on oi.deal_id = wo.id set lp.status = ?? where ord.order_type = 1 and DATEDIFF(CURDATE(), FROM_UNIXTIME(lp.created_at)) = wo.returnWithin; 是占位符。