简体   繁体   English

mysql查询2张表的总和

[英]mysql query on 2 tables with sum

I am having some difficulty generating a query that works in the situation I am in. Here are the details... 我在生成适合我所处情况的查询时遇到了一些困难。以下是详细信息...

1 table = billing 1张桌子=账单
1 table = billing dates 1张表=帐单日期

billing = basically when an invoice is generated, it creates a row in billing with a primary key, an invoice id, the users id, the users username, their actual name, the invoice date, the invoice total, and the payments made billing =基本上是在生成发票时,它会在计费中创建一行,其中包含主键,发票ID,用户ID,用户用户名,他们的真实姓名,发票日期,发票总额以及已付款

billing dates = when a payment is made to an invoice, it creates a row in billing dates with a primary key, an id (which is the same as the primary key in billing table), an invoice id (same as the invoice id in billing), the users id (same as users id in billing), the date paid, the amount paid 帐单日期=付款时,它会在帐单日期中创建一行,并带有一个主键,一个ID(与计费表中的主键相同),一个发票ID(与发票中的发票ID相同)帐单),用户ID(与帐单中的用户ID相同),付款日期,付款金额

I am trying to create an ageing report that will pull each outstanding invoice and display it in a 30/60/90/90+ table in PHP to the end user. 我正在尝试创建一个老化报告,该报告将提取每张未结清的发票并将其显示在PHP的30/60/90/90 +表中,以显示给最终用户。 So if invoice #12 has a $100 balance on it, along with two $10 payments and the invoice is 60 days old, it would show this info in the report. 因此,如果12号发票上有100美元的余额,以及两次10美元的付款,并且该发票已使用60天,它将在报告中显示此信息。 Here is what I have... 这就是我所拥有的...

 $sql17 = "SELECT 
 $mysql_billing.login_id, $mysql_billing.primary_key, $mysql_billing.a_name,
 SUM($mysql_billing.custotal) AS finaltotal,
 SUM($mysql_billing_dates.amount) AS paidtotal,
 $mysql_billing.custotal - $mysql_billing_dates.amount AS total
 FROM $mysql_billing
 LEFT JOIN $mysql_billing_dates ON $mysql_billing.login_id = $mysql_billing_dates.login_id
 GROUP BY login_id
 ORDER BY a_name ASC";

when I run that, some are correct, while most are not. 当我运行它时,有些是正确的,而大多数不是。 I cannot figure out what the inconsistency is. 我无法弄清不一致之处。 Any ideas would be greatly appreciated and maybe I am going down the wrong road? 任何想法将不胜感激,也许我走错了路? MORE 更多
When I do the following the correct values show... 当我执行以下操作时,将显示正确的值...

 SELECT 
 $mysql_billing.login_id, $mysql_billing.primary_key, $mysql_billing.a_name,
 SUM($mysql_billing.custotal) AS finaltotal
 FROM $mysql_billing
 GROUP BY login_id
 ORDER BY a_name ASC

but in the original example, it doesnt always pull the correct value? 但是在原始示例中,它是否总是提取正确的值?

Okay, I believe I understand what's happening. 好吧,我相信我知道发生了什么事。

So, presumably either table can have more than one record per login_id, which is why you're doing the sum and group by. 因此,大概每个表的每个login_id都可以有多个记录,这就是为什么要进行求和和分组的原因。 However, take a look at what your query gives you back when you take out the aggregates and the group by and I think you may understand the problem. 但是,看看当您取出汇总和分组依据时查询返回的内容,我认为您可能会理解问题。

Let's say you have table 1 which has 2 records for login_id xxx1234 and table 2 which has 3 records for that login_id. 假设您有一个表1,其中有2个记录用于login_id xxx1234,表2则具有3个有关该login_id的记录。 Then when you left join them, you will get 6 records . 然后,当您离开加入他们时,您将获得6条记录

eg 例如

table 1:
  login, custotal
  xxx111, 10
  xxx111, 20

table 2:
  login, amount
  xxx111, 30
  xxx111, 40
  xxx111, 50

after the join:
  login, custotal, amount
  xxx111 10, 30
  xxx111 10, 40
  xxx111 10, 50
  xxx111 20, 30
  xxx111 20, 40
  xxx111 20, 50

So you will be potentially getting several times the amounts you expected in your sums. 因此,您将有可能获得预期金额的几倍。

What you instead want to do is something like this: 相反,您想要做的是这样的:

 SELECT 
 billing_totals.login_id, billing_totals.primary_key, billing_totals.a_name, 
 billing_totals.finaltotal,
 billing_dates_totals.paidtotal,
 (billing_totals.finaltotal - billing_dates_totals.paidtotal) AS total
 FROM
   (SELECT $mysql_billing.login_id, 
   $mysql_billing.primary_key, 
   $mysql_billing.a_name, 
   SUM($mysql_billing.custotal) AS finaltotal 
   FROM $mysql_billing GROUP BY login_id) billing_totals 
 LEFT JOIN 
   (SELECT $mysql_billing_dates.login_id, 
   SUM($mysql_billing_dates.amount) AS paidtotal 
   FROM $mysql_billing_dates GROUP BY login_id) billing_dates_totals 
 ON (billing_totals.login_id = billing_dates_totals.login_id) 
 ORDER BY a_name ASC";

Probably need to tweak that slightly as I don't have a mysql to test it on at the moment. 可能需要稍作调整,因为我目前没有mysql对其进行测试。

$sql17 = "SELECT $mysql_billing.login_id, $mysql_billing.primary_key, $mysql_billing.a_name, SUM($mysql_billing.custotal) AS finaltotal, SUM($mysql_billing_dates.amount) AS paidtotal, SUM($mysql_billing.custotal) - SUM($mysql_billing_dates.amount) AS total FROM $mysql_billing LEFT JOIN $mysql_billing_dates ON $mysql_billing.login_id = $mysql_billing_dates.login_id GROUP BY login_id ORDER BY a_name ASC";

EDIT : I tried some thing like this for u. 编辑:我为你尝试了类似的事情。

SELECT a.id, (select sum(custotal) from `billing` where id=a.id) as total1,(select sum(amount) 
from `billing_dates`
where id=a.id) as total1 from `billing` a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM