我有两个表,分别为finance_mpc_budget
和finance_mpc_issues
。他们加入并符合以下条件-
finance_mpc_budget.mpc_budget_id = finance_mpc_issues.ref_no
因此,我需要使用
更新finance_mpc_issues.ref_no
finance_mpc_budget.mpc_budget_id where finance_mpc_budget.mpc_budget_id > 55
为此,我使用了以下查询
UPDATE finance_mpc_issues
JOIN (
SELECT mpc_budget_id FROM finance_mpc_budget
WHERE finance_mpc_budget.mpc_budget_id > 55
ORDER BY mpc_budget_id) a
ON a.mpc_budget_id = finance_mpc_issues.ref_no
SET finance_mpc_issues.ref_no = a.mpc_budget_id
但是它生成了空结果集。可能是错的。谁能帮我吗?
+---------------+----------+--------+
| mpc_budget_id | category | amount |
+---------------+----------+--------+
| 56| A | 22000 |
| 57| B | 25000 |
| 58| C | 45000 |
| 59| D | 16000 |
+---------------+----------+--------+
+-----------+--------+--------+
| issues_id | ref_no | amount |
+-----------+--------+--------+
| 10 | | 22000 |
| 11 | | 25000 |
| 12 | | 45000 |
| 13 | | 16000 |
+-----------+--------+--------+
+-----------+--------+--------+
| issues_id | ref_no | amount |
+-----------+--------+--------+
| 10 | 56| 22000 |
| 11 | 57| 25000 |
| 12 | 58| 45000 |
| 13 | 59| 16000 |
+-----------+--------+--------+
答案 0 :(得分:0)
您可以在下面尝试-
update finance_mpc_issues
join finance_mpc_budget a on a.mpc_budget_id = finance_mpc_issues.ref_no
set finance_mpc_issues.ref_no=a.mpc_budget_id
where a.mpc_budget_id >55
答案 1 :(得分:-1)
您不能join
使用ref_no
,因为它为空。因此,您的finance_mpc_issues
表未更新。
根据您的表和数据,我假定 amount
列具有相同的值。因此,使用amount
列可以得到所需的输出。
Update finance_mpc_issues fi
set fi.ref_no =
(select fb.mpc_budget_id
from finance_mpc_budget fb
WHERE fb.amount = fi.amount
);