MySQL:使用Join和Union从单行中获取多个表的数据

发布于 2025-01-22 14:09:16 字数 2633 浏览 0 评论 0原文

我有三个数据库表“客户”,“帐单”和“交易”

客户:

cuid名称地址
1DavidCity 1 2
Roja City 2RojaCity 2

帐单:< /strong>

idcuidbill_amount
111 1100
212200
321400
413100

交易:

idcuiddaterecceede_amount
112022-3-3-02250
2---2022-2-2-2-2 02200
322022-3-02200

我需要一个新生成的应有桌子,在计算月份的适当金额后使用这样的fifo:

cuidname namedamedodairdue_amount
1DavidCity 1250
1David City 1 DavidCity 1 David City 13100

此代码无法正常工作。 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
1DavidCity 11150
2RojaCity 210

I have three database tables ‘customer’, ‘billing’, and ‘transaction’

customer:

cuidnameaddress
1DavidCity 1
2RojaCity 2

billing :

idcuidmonthbill_amount
111100
212200
321400
413100

transaction:

idcuiddatereceived_amount
112022-3-02250
222022-2-02200
322022-3-02200

I need a new generated Due table after calculating month wise due amount using FIFO like this:

cuidNameAddressMonthDue_Amount
1DavidCity 1250
1DavidCity 13100

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 :

cuidNameAddressMonthDue_Amount
1DavidCity 11150
2RojaCity 210

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-29 14:09:16

您可能不需要所有这些sum()联合所有和子查询。您可以在单个查询中进行操作,加入这样的所有三个表:

SELECT c.cuid,
       c.Name,
       c.Address,
       b.Month,
       t.received_amount-b.bill_amount AS Due_Amount
FROM customer c 
  JOIN billing b 
    ON c.cuid=b.cuid
  JOIN transaction t 
    ON c.cuid=t.cuid
    AND b.month=MONTH(t.date);

演示小提琴

我确实有一些好奇心,因为我对due_amount的理解是客户需要支付剩余的金额,而您的您似乎不是一个。如果是这样,则该值应该为负,因为客户Davidbill_amount付费更多(receed_amount)。

You probably don't need all that SUM(), UNION ALL and subquery. You can do it in a single query and JOIN all three tables like this:

SELECT c.cuid,
       c.Name,
       c.Address,
       b.Month,
       t.received_amount-b.bill_amount AS Due_Amount
FROM customer c 
  JOIN billing b 
    ON c.cuid=b.cuid
  JOIN transaction t 
    ON c.cuid=t.cuid
    AND b.month=MONTH(t.date);

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 customer David paid more (received_amount) than the bill_amount.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文