我正在尝试编写一个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
但唯一可以返回的是我的发票表中的一堆随机行....有关如何解决此问题的任何想法?
任何正确方向的帮助或步骤都将是一个巨大的帮助。
答案 0 :(得分: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