从sql中的多个表中获取数据

时间:2016-11-08 11:25:29

标签: php mysql sql

表1:发票(inv_id,inv_value,cust_id)
表2:客户(cust_id,sales_rep)
表3:成员(Member_id,member_cateogry,member_type,cust_id)

注1:每位客户支付多个发票。 (一对多关系)。
注2:每位客户支付一个或多个成员的费用(因此一个以上的成员可能与一个客户有关) 注3:每个成员的类别可以是1“代表个人”或2“代表集团” 注4:每个成员的类型可以是1“代表新”或2“代表续订”。

对于Invoice_value = 1且sales_rep = 10且member_category = 123的客户,我想获得members_type字段的总计 例如:如果这些客户的Sales_rep为1,并且他们支付的成员是新成员和个人成员,则客户支付的发票总额是多少。

我试过了:

SELECT Sum(invoices.inv_value) AS total
FROM   invoices,
       customers,
       members
WHERE  invoices.cust_id = customers.cust_id
AND    members.custid = customers.cust_id
AND    members.category = {$category}
AND    members_type = {$type}
AND    customers.sales_rep = {$id}";

AND

SELECT     Sum(invoices.inv_value) AS total
FROM       members
INNER JOIN customers
ON         members.custid = customers.cust_id
INNER JOIN invoices
ON         customers.cust_id = invoices.cust_id
WHERE      customers.sales_rep = {$id}
AND        members.category = {$category}
AND        members.type = {$type}";

但两者都返回了发票价值的两倍 例如:1发票表中的120美元发票使用这些SQL查询返回$ 240。

为什么会发生这种情况以及如何修复它? 提前致谢

2 个答案:

答案 0 :(得分:1)

这是您的查询:

SELECT sum(i.inv_value) as total
FROM members m INNER JOIN
     customers c
     ON m.custid = c.cust_id INNER JOIN
     invoices i
     ON c.cust_id = i.cust_id
WHERE c.sales_rep = {$id} AND 
      m.category = {$category} AND
      m.type = {$type}";

(不要使用逗号使用隐式JOIN语法。它过时且功能不强。)

问题可能是两个成员可以拥有相同的客户ID。您可以通过运行来检查:

select m.cust_id, count(*)
from members m
group by m.cust_id
having count(*) > 1;

客户ID也可能在customers中重复。

假设重复项仅在members中,请将查询更改为exists

SELECT sum(i.inv_value) as total
FROM customers c
     ON  INNER JOIN
     invoices i
     ON c.cust_id = i.cust_id
WHERE c.sales_rep = {$id} AND 
      EXISTS (SELECT 1
              FROM members m
              WHERE m.custid = c.cust_id AND
                    m.category = {$category} AND
                    m.type = {$type}
             );

答案 1 :(得分:0)

您似乎从inv_value开始invoicescustomerscustomermembers表格有{strong>多对一关系,invoices.cust_id invoices.inv_value custid1 100 1}}表有一对多关系。

假设您有以下数据

发票表

customer.cust_id
custid1

客户表

members.cust_id     members.category
custid1                 1
custid1                 2

会员表

customer.cust_id    members.cust_id         invoices.inv_value  members.category
custid1                 custid1                 100                 1
custid1                 custid1                 100                 2

加入所有三个表格

custid1

如果您注意到2 member_category中存在invoice value,则SELECT Sum(invoices.inv_value) AS total FROM( SELECT DISTINCT members.custid, invoices.inv_value inv_value FROM members INNER JOIN customers ON members.custid = customers.cust_id INNER JOIN invoices ON customers.cust_id = invoices.cust_id WHERE customers.sales_rep = {$id} AND members.category = {$category} AND members.type = {$type}); 也会重复。

要解决此问题,首先您可以获取不同的记录,然后汇总这些不同的记录将帮助您解决问题,如下所示

<强>解决方案

public static void main(String[] args) {

    String test = Strings.repeat("1", 52);

    double first = 0.5;
    double result = 0.0;
    for (char c : test.toCharArray()) {
        result += first;
        first = first / 2;
    }

    System.out.println(result); // close approximation of 1
    System.out.println(Math.pow(2, 1023) * (1 + result));
    System.out.println(Double.MAX_VALUE);

} 

希望这能解决您的问题