我有一个查询,该查询在运行时提供令人满意的结果,该查询如下:-
SELECT DISTINCT(cp.payment_ref),COUNT(cp.status) as 'Amount Declined',cp.*
FROM client_payments as cp
WHERE DATE(cp.payment_date) = CURDATE()
GROUP BY cp.payment_ref
HAVING COUNT(cp.status='Declined')=3
我想要做的是使用标签更新查询的结果,所以我要做的就是将查询转换为更新查询,以更新结果集。我无法使它正常工作..这是我的最佳尝试
UPDATE client_payments SET retry_status = 'retry' WHERE id=(SELECT cp.id ,COUNT(cp.status)
FROM client_payments as cp
WHERE DATE(cp.payment_date) = '2018-09-24'
GROUP BY cp.payment_ref
HAVING COUNT(cp.status='Declined')=3)
这会引发错误#1093 - You can't specify target table 'client_payments' for update in FROM clause
有什么想法吗?
答案 0 :(得分:0)
您需要来自条款:
UPDATE client_payments SET client_payments.retry_status = 'retry'
from(SELECT cp.id ,COUNT(cp.status)
FROM client_payments as cp
WHERE DATE(cp.payment_date) = '2018-09-24'
GROUP BY cp.payment_ref
HAVING COUNT(cp.status='Declined')=3) as myquery
WHERE client_payments.id=myquery.id
答案 1 :(得分:0)
因此可行的解决方案如下:-
UPDATE client_payments as cp1
INNER JOIN (SELECT cp.id ,COUNT(cp.status)
FROM client_payments as cp
WHERE DATE(cp.payment_date) = '2018-09-24'
GROUP BY cp.payment_ref
HAVING COUNT(cp.status='Declined')=3) as cp2 on cp1.id = cp2.id
SET cp1.retry_status = 'retry'
感谢穆罕默德的{动和this post中的mellamokb的启发。