多个选择条件来自每个表

时间:2017-09-13 14:24:34

标签: mysql

所以我有桌子 ip_clients,ip_invoice_items,ip_invoice_item_amounts,ip_invoices,ip_payments,ip_tax_rates。

结构:

| ip_clients  | ip_invoice_items     | ip_invoice_item_amounts |
| ----------- | -------------------- | ----------------------- |
| client_id   | item_id              | item_amount_id          |
| client_name | invoice_id           | item_id                 |
|             | item_quantity        | item_total              |
|             | item_price           | item_discount           |
|             | item_discount_amount |                         |
| ----------- | -------------------- | ----------------------- |

| ip_invoices       | ip_payments    | ip_tax_rates     |
| ----------------- | -------------- | ---------------- |
| invoice_id        | payment_id     | tax_rate_percent |
| user_id           | invoice_id     |                  |
| invoice_status_id | payment_amount |                  |
| ----------------- | -------------- | ---------------- |

表和单元格中存储的内容:

ip_clients :存储客户的地方

client_id: client unique id
client_name: the client name

ip_invoice_items :发票项目存放的位置

item_id: unique item id
invoice_id: unique invoice id from ip_invoices
item_quantity: the item's quantity
item_price: the item's cost stored as double example (11.29)
item_discount_amount: the item's discount stored as double example (5.19)

ip_invoice_item_amounts :付款的存储位置

item_amount_id: the unique the item's id
item_id: the unique item's id from ip_invoice_items
item_total: the total item cost is stored as double (50.01)
item_discount: the item discount is stored as double (30.49)

ip_invoices :发票ID和用户ID

invoice_id: the unique invoice id
user_id: the unique user id from ip_clients
invoice_status_id: if this cell has number 4 means its paid

ip_payments :付款ID为发票ID和付款金额

payment_id: the unique payment id
invoice_id: the unique invoice id from ip_invoice
payment_amount: the amount paid is stored as double (100.00)

ip_tax_rates :包含税号百分比

tax_rate_percent: contains single number

工作原理:

  1. 我使用所选项目金额和费用创建发票
  2. 我从客户处插入付款以关闭发票。
  3. 问题

    我想知道客户的余额,他需要支付多少钱 我希望得到客户名称,并且只有当他有未结发票(他拥有钱)时才能获得当前债务(余额)
    因此,查询应该能够查看已支付的发票ID,并且不计算这些金额,并且在余额结果中应添加税率。

    我很抱歉我无法做到这一点我被卡住了,我甚至不知道我是否正确解释了因为这对我来说很复杂我请求帮助

    这是结构https://pastebin.com/c3iYNMkq

1 个答案:

答案 0 :(得分:0)

首先,让我们找出某个客户的公开发票,其中包含每个客户的债务:

select ip_clients.client_id, sum(ip_invoice_items.item_quantity * ip_invoice_items.item_price) - sum(ip_payments.payment_amount) as debt
from ip_clients
join ip_invoices
on ip_clients.client_id = ip_invoices.user_id
join ip_invoice_items
on ip_invoices.invoice_id = ip_invoice_items.invoice_id
join ip_payments
on ip_invoices.invoice_id = ip_payments.invoice_id
group by ip_clients.client_id, ip_invoices.invoice_id
having sum(ip_invoice_items.item_quantity * ip_invoice_items.item_price) - sum(ip_payments.payment_amount) > 0

现在,让我们使用这些知识来获得总余额:

select ip_clients.client_name, sum(debt) as balance
from ip_clients
join
(
select ip_clients.client_id, sum(ip_invoice_items.item_quantity * ip_invoice_items.item_price) - sum(ip_payments.payment_amount) as debt
from ip_clients
join ip_invoices
on ip_clients.client_id = ip_invoices.user_id
join ip_invoice_items
on ip_invoices.invoice_id = ip_invoice_items.invoice_id
join ip_payments
on ip_invoices.invoice_id = ip_payments.invoice_id
group by ip_clients.client_id, ip_invoices.invoice_id
having sum(ip_invoice_items.item_quantity * ip_invoice_items.item_price) - sum(ip_payments.payment_amount) > 0
) t
on ip_clients.client_id = t.client_id
group by ip_clients.client_id, ip_clients.client_name

我不是100%确定我理解您的商业模式,因此您可能会略微改进查询,但您明白了。