复杂的MySQL查询错误的结果

时间:2011-10-12 07:41:56

标签: mysql

我正在尝试构建复杂的mysql查询,但它返回错误的结果......

SELECT
  b.name  AS batch_name,
  b.id    AS batch_id,

  COUNT(DISTINCT s.id)
    AS total_students,

  COALESCE( SUM(s.open_bal), 0 )
    AS open_balance,

  SUM(  COALESCE(i.reg_fee,   0)
      + COALESCE(i.tut_fee,   0)
      + COALESCE(i.other_fee, 0)
  ) AS gross_fee,

  SUM( COALESCE(i.discount, 0) )
    AS discount,

  COALESCE( SUM(s.open_bal), 0 )
    + SUM(  COALESCE(i.reg_fee,   0)
          + COALESCE(i.tut_fee,   0)
          + COALESCE(i.other_fee, 0)
      )
    - SUM( COALESCE(i.discount, 0) )
    AS net_payable,

  SUM(  COALESCE(r.reg_fee,   0)
      + COALESCE(r.tut_fee,   0)
      + COALESCE(r.other_fee, 0)
  ) AS net_recieved,

  ( COALESCE( SUM(s.open_bal), 0 )
    + SUM(  COALESCE(i.reg_fee,   0)
          + COALESCE(i.tut_fee,   0)
          + COALESCE(i.other_fee, 0)
      )
    - SUM(  COALESCE(i.discount,  0) )
  )
  - ( SUM(  COALESCE(r.reg_fee,   0)
          + COALESCE(r.tut_fee,   0)
          + COALESCE(r.other_fee, 0)
      )
    )
    AS balance_due

  FROM batches b
  LEFT JOIN students s ON s.batch      = b.id
  LEFT JOIN invoices i ON i.student_id = s.id
  LEFT JOIN recipts  r ON r.student_id = s.id

  WHERE s.inactive = 0
  GROUP BY b.name, b.id;

返回以下结果......

| batch_name | total_students  | open_bal | gross_fee | discount | net_payable | net_recieved | due_balance |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+
|  MS        | 6               | 10000    | 0         | 0        | 10000       | 101000       | -91000      |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+

批量表

| id  | name |
+-----+------+
| 9   | Ms   |
+-----+------+

学生表

| id  | open_bal | batch | inactive |
+-----+----------+-------+----------+
| 44  | -16000   | 9     | 0        |
+-----+----------+-------+----------+
| 182 | 9000     | 9     | 0        |
+-----+----------+-------+----------+
| 184 | -36000   | 9     | 0        |
+-----+----------+-------+----------+
| 185 | 19000    | 9     | 0        |
+-----+----------+-------+----------+
| 186 | 9000     | 9     | 0        |
+-----+----------+-------+----------+
| 187 | 4000     | 9     | 0        |
+-----+----------+-------+----------+

发票表

| id   | student_id | reg_fee | tut_fee | other_fee | net_payable | discount |
+------+------------+---------+---------+-----------+-------------+----------+
|      |            |         |         |           |             |          |
+------+------------+---------+---------+-----------+-------------+----------+

上述学生ID没有发票。

收件表

| id   | student_id | reg_fee | tut_fee | other_fee | status     |
+------+------------+---------+---------+-----------+------------+
|  8   | 44         | 0       | 0       | 1500      |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  277 | 44         | 0       | 50000   | 0         |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  26  | 182        | 0       | 0       | 1500      |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  424 | 182        | 0       | 15000   | 0         |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  468 | 182        | 0       | 15000   | 0         |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  36  | 185        | 0       | 0       | 1500      |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  697 | 185        | 0       | 15000   | 0         |  confirmed |
+------+------------+---------+---------+-----------+------------+
|  66  | 187        | 0       | 0       | 1500      |  confirmed |
+------+------------+---------+---------+-----------+------------+

使用上述sql查询和表格的预期结果......

| batch_name | total_students  | open_bal | gross_fee | discount | net_payable | net_recieved | due_balance |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+
|  MS        | 6               | -11000   | 0         | 0        | 10000       | 101000       | -112000     |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+

2 个答案:

答案 0 :(得分:1)

你仍然没有提供完整的信息 - 没有批处理表,甚至没有现有的recipts表..无论如何,我认为我们不关心批处理表中的什么,让我们说它只是名称和ID。您的收据表为同一个学生提供了多行。由于所有JOIN,这应该导致为其他表返回多行。因此,SUM()多次为必须求和的值,即open_balance。这可能是问题出在何处的线索,我说你必须将你需要的信息从收据表移动到子查询中,但我不确定你是否向我们展示了你的整个数据库。尝试从查询中删除收据表并再次检查结果。如果就是这样,你应该看看从那里开始做什么或至少给我们更多的信息。

修改 查询应为:

SELECT
  b.name  AS batch_name,
  b.id    AS batch_id,

  COUNT(DISTINCT s.id)
    AS total_students,

  COALESCE( SUM(s.open_bal), 0 )
    AS open_balance,

  SUM(  COALESCE(i.reg_fee,   0)
      + COALESCE(i.tut_fee,   0)
      + COALESCE(i.other_fee, 0)
  ) AS gross_fee,

  SUM( COALESCE(i.discount, 0) )
    AS discount,

  COALESCE( SUM(s.open_bal), 0 )
    + SUM(  COALESCE(i.reg_fee,   0)
          + COALESCE(i.tut_fee,   0)
          + COALESCE(i.other_fee, 0)
      )
    - SUM( COALESCE(i.discount, 0) )
    AS net_payable,
  SUM((SELECT SUM(COALESCE(receipts.reg_fee,   0)
      + COALESCE(receipts.tut_fee,   0)
      + COALESCE(receipts.other_fee, 0)) FROM receipts WHERE receipts.student_id = s.id))
  AS net_recieved,

  ( COALESCE( SUM(s.open_bal), 0 )
    + SUM(  COALESCE(i.reg_fee,   0)
          + COALESCE(i.tut_fee,   0)
          + COALESCE(i.other_fee, 0)
      )
    - SUM(  COALESCE(i.discount,  0) )
  ) 
  - SUM((SELECT SUM(COALESCE(receipts.reg_fee,   0)
          + COALESCE(receipts.tut_fee,   0)
          + COALESCE(receipts.other_fee, 0)) FROM receipts WHERE receipts.student_id = s.id)) 
    AS balance_due

  FROM batches b
  LEFT JOIN students s ON s.batch      = b.id
  LEFT JOIN invoices i ON i.student_id = s.id
  WHERE s.inactive = 0
  GROUP BY b.name, b.id;

这将汇总收据表中的学生数据,即使它在多行上,只返回一行。将联接删除到收据表将从其他表中删除重复的行,因此现在计算应该是正确的。

还有一件事 - 你在WHERE子句中得到s.inactive = 0,确保它与这个计算无关。

P.S。你怎么不知道子查询是什么,你最终写这样的东西?

答案 1 :(得分:0)

我已经得到了解决方案,我一起加入了大量的查询,而且一些结果是加倍的。感谢。