使用SQL更新连接表

时间:2018-02-07 06:35:02

标签: sql postgresql

这是我的选择查询

select s.enabled,
       s.totalpoints,
       transaction_diff_dates
from loyalty_scheme l
  join subscribe_merchant s on (l.scheme_id = s.schemerefid)
  join transaction t on (s.unipoint_customer_ref_id = t.unipoint_customer_ref_id)
where (s.transaction_diff_dates = to_char(current_date, 'yyyy-MM-dd'))
  and (s.merchant_ref_id = 11)
  and (s.schemerefid = (select scheme_id
                        from loyalty_scheme
                        where (minimum_purchase_amount = (select min(minimum_purchase_amount)
                                                          from loyalty_scheme
                                                          where merchant_id = 11))
                        group by scheme_id))
group by s.unipoint_customer_ref_id,
         s.enabled,
         s.totalpoints,
         s.transaction_diff_dates

此查询返回两行。

我需要更新此启用的字段=' false' ,并且totalPoint = 0已归档,而不是选择

所以我试着写一个更新查询

UPDATE subscribe_merchant 
SET enabled = 'true'  
FROM loyalty_scheme L 
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) 
WHERE (s.transaction_diff_dates = to_char(CURRENT_DATE, 'yyyy-MM-dd')) 
  AND (s.merchant_ref_id = 11) 
  AND (s.schemerefid = (SELECT scheme_id   
                        FROM loyalty_scheme 
                        WHERE (minimum_purchase_amount = (SELECT MIN(minimum_purchase_amount) 
                                                          FROM loyalty_scheme 
                                                          WHERE merchant_id = 11))
                        GROUP BY scheme_id))
GROUP BY 
    s.unipoint_customer_ref_id, s.enabled, 
    s.totalpoints, s.transaction_diff_dates 

但此更新查询不起作用。

请帮我写一下更新查询

0 个答案:

没有答案