我有下表管理收入
收入表
+------------+--------+--------+
| revenue_id | amount | status |
+------------+--------+--------+
| 1 | 45000 | 1 |
| 2 | 25000 | 1 |
| 3 | 67000 | 1 |
| 4 | 22000 | 1 |
| 5 | 32000 | 0 |
+------------+--------+--------+
银行桌
+---------+--------+-------------+-------------+
| bank_id | ref_no | bank_amount | bank_status |
+---------+--------+-------------+-------------+
| 1 | 2 | 23000 | Pending |
| 2 | 3 | 67000 | Confirmed |
| 3 | 4 | 22000 | Confirmed |
+---------+--------+-------------+-------------+
02)如果收入表中提到的收入已经存入银行,则将其记录在银行表中。之后,两个表中的金额相等,银行状态可能会变成“已确认”。
03)因此,我需要通过连接上面的两个表来获取确认记录,如下所示:
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 3 | 67000 | 67000 | Confirmed |
| 4 | 22000 | 22000 | Confirmed |
+------------+--------+-------------+-------------+
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 1 | 45000 | | |
| 2 | 25000 | 23000 | Pending |
| 3 | 67000 | 67000 | Confirmed |
| 4 | 22000 | 22000 | Confirmed |
+------------+--------+-------------+-------------+
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 1 | 45000 | | |
| 2 | 25000 | 23000 | Pending |
+------------+--------+-------------+-------------+
04)为了获得所需的输出,我使用了以下查询
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from revenue
left join bank on bank.ref_no = revenue.revenue_id
where revenue.status = 1 and bank.bank_status = "Confirmed"
05)但是没有得到预期的结果。它仅生成空结果。我不明白我要怎么做。有人可以帮我吗?
答案 0 :(得分:0)
您必须使用内部联接而不是左联接来获得预期的输出。
SELECT revenue.revenue_id,
revenue.amount,
bank.bank_amount,
bank.bank_status
FROM revenue
INNER JOIN bank ON bank.ref_no = revenue.revenue_id
WHERE revenue.status = 1
AND bank.bank_status = "Confirmed"
答案 1 :(得分:0)
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from revenue
Inner join (select *
From bank
Where bank_status = "Confirmed") bank
on bank.ref_no = revenue.revenue_id
答案 2 :(得分:0)
对于左联接,必须使用第一个银行表,然后使用收入表。 因为在左连接中,即使没有匹配项,也将返回所有行。
因此,使用这样的查询。 选择Revenue.revenue_id,report.amount,bank.bank_amount,bank.bank_status 从银行 bank.ref_no =收入.revenue_id上的左加入收入 其中Revenue.status = 1,bank.bank_status =“已确认”