从报告mysql查询中删除重复值

时间:2013-07-02 02:56:34

标签: mysql sql

需要运行一个查询,该查询输出给定期间(即一个月)的报告,并显示向客户收取的所有费用和支付的金额(如果有)。我的问题是显示该客户每行支付的金额。我想显示所有费用和一个付款金额(让我们说最后一行)。不要为客户显示重复的付款金额


Last NameFirst NameMIDate of ServiceFeePayment
 BarrazaReyD5/1/2013$160.00 $0.00 
 BarrazaReyD5/1/2013$12.10 $0.00 
 OteroAliciaM5/3/2013$84.68 $329.20 
 OteroAliciaM5/3/2013$99.46 $329.20 
 OteroAliciaM5/3/2013$46.59 $329.20 
 OteroAliciaM5/3/2013$48.98 $329.20 
 OteroAliciaM5/3/2013$26.35 $329.20 
 OteroAliciaM5/3/2013$23.14 $329.20 
 Mayweather Bethy 5/5/2013$99.46 $249.70 
 Mayweather Bethy 5/5/2013$97.96 $249.70 
 Mayweather Bethy 5/6/2013$34.73 $249.70 
 Mayweather Bethy 5/6/2013$17.55 $249.70 
 GarciaMichael 5/11/2013$198.92 $0.00 
 GarciaMichael 5/11/2013$29.95 $0.00 
 GarciaMichael 5/11/2013$17.30 $0.00 
 SmithRichard 5/15/2013$35.00 $0.00 
 SmithRichard 5/15/2013$2.64 $0.00 
 BoothKeithR5/23/2013$14.64 $0.00 
 BoothKeithR5/23/2013$255.30 $0.00 
 BoothKeithR5/23/2013$66.99 $0.00 
 SmithVanessa 5/23/2013$18.00 $0.00 
 SmithVanessa 5/23/2013$1.36 $0.00 
 HERNANDEZLAURA 5/25/2013$34.47 $0.00 
 MyersJonathan 5/30/2013$35.00 $37.65 
 MyersJonathan 5/30/2013$2.65 $37.65 


显示向客户收取的每笔费用的总金额。我的查询是

$query = "SELECT
 c.lname as lname,
 c.fname as fname,
 c.mname as mname,
 s.date as date,
 b.fee as fee,
 p.amount as payment,
 FROM sell as s,
 customer as c,
 billing b
 LEFT JOIN payments p on p.completion = b.completion AND p.cid=b.cid
 WHERE      
 s.completion = b.completion
 AND b.cid = s.cid
 AND c.pid = s.cid
 AND s.date BETWEEN ? and ?
 ORDER BY s.date";

我尝试了GROUP BY p.amount,但只为该客户留下了一行并削减了所有其他费用。我需要显示向客户收取的所有费用,但只需一个金额作为付款。请帮忙

2 个答案:

答案 0 :(得分:0)

您在查询中混合了连接样式(ANSI连接和逗号连接)。不要那样做。尝试始终使用ANSI JOIN表示法。它更具描述性。

话虽如此,我相信您的查询应该看起来像

SELECT c.lname,
       c.fname,
       c.mname,
       s.date,
       b.fee,
       p.amount payment
  FROM sell s JOIN customer c
    ON s.cid = c.pid JOIN billing b
    ON s.cid = b.cid 
   AND s.completion = b.completion LEFT JOIN payments p 
    ON b.completion = p.completion 
   AND b.cid = p.cid
 WHERE s.date BETWEEN ? AND ?
 ORDER BY s.date

查询显然尚未经过测试

如果这不能解决您的问题,您必须编辑您的问题并为所有涉及的表(几行)发布相关的样本数据,以便在同一个表格中生成所需的输出(您已经发布)形式。

答案 1 :(得分:0)

要了解给定客户的每笔费用的详细信息,@ peterm解决方案应该可以正常工作。

您仍然缺少每位客户的总付款。为此,您需要单独进行查询:

SELECT c.cid,
       SUM(p.amount) AS payment_total
  FROM sell s JOIN customer c
  ... all necessary joins here
  GROUP BY c.cid