MySQL:使用Join和Union从单行中获取多个表的数据
我有三个数据库表“客户”,“帐单”和“交易”
客户:
cuid | 名称 | 地址 |
---|---|---|
1 | David | City 1 2 |
Roja City 2 | Roja | City 2 |
帐单:< /strong>
id | cuid | 月 | bill_amount |
---|---|---|---|
1 | 1 | 1 1 | 100 |
2 | 1 | 2 | 200 |
3 | 2 | 1 | 400 |
4 | 1 | 3 | 100 |
交易:
id | cuid | date | recceede_amount |
---|---|---|---|
1 | 1 | 2022-3-3-02 | 250 |
2 | --- | 2022-2-2-2-2 02 | 200 |
3 | 2 | 2022-3-02 | 200 |
我需要一个新生成的应有桌子,在计算月份的适当金额后使用这样的fifo:
cuid | name name | dame | dodair | due_amount |
---|---|---|---|---|
1 | David | City 1 | 2 | 50 |
1 | David City 1 David | City 1 David City 1 | 3 | 100 |
此代码无法正常工作。 MySQL代码:
SELECT
due.cuid,
due.Name,
due.Address,
due.Month,
sum(due.Amount - due.Received) AS Due_Amount
FROM
(SELECT
c.cuid,
c.name AS name,
c.address AS address,
b.month AS Month,
0 AS Received,
b.bill_amount AS Amount
FROM
customer c
INNER JOIN billing b ON c.cuid=b.cuid
UNION ALL
SELECT
c.cuid,
c.name AS Name,
c.address AS Address,
null AS Month,
t.received_amount AS Received,
0 AS Amount
FROM
customer c
INNER JOIN transaction t ON c.cuid = t.cuid) AS due
GROUP BY
due.cuid;
生成的表格是:
cuid | 姓名 | 地址 | 月 | day_amount |
---|---|---|---|---|
1 | David | City 1 | 1 | 150 |
2 | Roja | City 2 | 1 | 0 |
I have three database tables ‘customer’, ‘billing’, and ‘transaction’
customer:
cuid | name | address |
---|---|---|
1 | David | City 1 |
2 | Roja | City 2 |
billing :
id | cuid | month | bill_amount |
---|---|---|---|
1 | 1 | 1 | 100 |
2 | 1 | 2 | 200 |
3 | 2 | 1 | 400 |
4 | 1 | 3 | 100 |
transaction:
id | cuid | date | received_amount |
---|---|---|---|
1 | 1 | 2022-3-02 | 250 |
2 | 2 | 2022-2-02 | 200 |
3 | 2 | 2022-3-02 | 200 |
I need a new generated Due table after calculating month wise due amount using FIFO like this:
cuid | Name | Address | Month | Due_Amount |
---|---|---|---|---|
1 | David | City 1 | 2 | 50 |
1 | David | City 1 | 3 | 100 |
This code did not work properly. MySql Code:
SELECT
due.cuid,
due.Name,
due.Address,
due.Month,
sum(due.Amount - due.Received) AS Due_Amount
FROM
(SELECT
c.cuid,
c.name AS name,
c.address AS address,
b.month AS Month,
0 AS Received,
b.bill_amount AS Amount
FROM
customer c
INNER JOIN billing b ON c.cuid=b.cuid
UNION ALL
SELECT
c.cuid,
c.name AS Name,
c.address AS Address,
null AS Month,
t.received_amount AS Received,
0 AS Amount
FROM
customer c
INNER JOIN transaction t ON c.cuid = t.cuid) AS due
GROUP BY
due.cuid;
Code generated table is :
cuid | Name | Address | Month | Due_Amount |
---|---|---|---|---|
1 | David | City 1 | 1 | 150 |
2 | Roja | City 2 | 1 | 0 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能不需要所有这些
sum()
,联合所有
和子查询。您可以在单个查询中进行操作,加入
这样的所有三个表:演示小提琴
我确实有一些好奇心,因为我对
due_amount
的理解是客户需要支付剩余的金额,而您的您似乎不是一个。如果是这样,则该值应该为负,因为客户David
比bill_amount
付费更多(receed_amount
)。You probably don't need all that
SUM()
,UNION ALL
and subquery. You can do it in a single query andJOIN
all three tables like this:Demo fiddle
I do have a slight curiosity though because my understanding of
Due_amount
is the remaining amount need to be paid by the customer and yours doesn't seem to be one. If it is, then the value should be negative because the customerDavid
paid more (received_amount
) than thebill_amount
.