1

我正在尝试编写一个 MySQL 查询,但我很难编写它,它涉及 3 个表!!!

table 1 - Invoice table - Has a client id, credit id, invoice id and commission subtotal
table 2 - Credits table - Has a client id, credit id and commission amount (same as commission subtotal)
table 3 - Agents table - Has a client id and agent id

我正在尝试从发票 ID 上的每个信用 ID 的信用表中创建代理佣金金额列表。

我真的希望这是有道理的。

这就是我所拥有的:

select cfi.commissionamount
        from invoice ii
        inner join clientagent cli
        on ii.client = cli.clientid
        inner join clientagent cl
        on ii.client = cl.clientid
        left outer join creditfee cfi
        on cfi.client = ii.client
        and (cfi.issuedate BETWEEN ii.startdate AND ii.enddate)
        where ii.invoiceid = cfi.invoiceid
        and cli.clientagentid = cl.clientagentid

但是唯一返回的是我的发票表中的一堆随机行......关于如何解决这个问题的任何想法?

任何帮助或朝着正确方向迈出的一步都将是巨大的帮助。

4

1 回答 1

1

我认为您正在寻找这个,但您需要回答上述问题,以便我们为您提供更多帮助并提供准确的表格结构,因为您的单元格名称中有空格。

SELECT cli.agentid, SUM(cfi.commissionamount)
    FROM invoice AS ii
        INNER JOIN clientagent AS cli
            ON ii.clientid = cli.clientid
        LEFT OUTER JOIN creditfee AS cfi
            ON cfi.clientid = ii.clientid AND (cfi.issuedate BETWEEN ii.startdate AND ii.enddate)
        WHERE ii.invoiceid = cfi.invoiceid AND cli.clientagentid = cl.clientagentid
        GROUP BY ii.clientid
于 2013-08-22T16:21:18.370 回答