需要mysql查询帮助

时间:2011-10-12 09:13:39

标签: mysql

  

可能重复:
  complex my sql query wrong results

我正在尝试构建复杂的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;

我做错了,因为sum(open_bal)结果是错误的。

示例结果......

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

以上结果有误,请查看以下表格结构

学生表

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

发票表

| 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     |
+------------+-----------------+----------+-----------+----------+-------------+--------------+-------------+

请检查并回复,谢谢。

1 个答案:

答案 0 :(得分:0)

您必须将student_id添加到GROUP BY表达式中。

您正在进行多个外连接,这意味着所有数据都将在查询中使用,但收据表中的某些学生也有几行,因此它们的数量将会重复(因为会有几个带有s.id的行。

查看以下联接:

        r.id     s.id     open_bal
        --------------------------
        8       | 44    | -16000  
        277     | 44    | -16000  
        26      | 182   |   9000  
        424     | 182   |   9000  
        468     | 182   |   9000  
        36      | 185   |  19000
        697     | 185   |  19000
        66      | 187   |   4000
        null    | 184   | -36000
        null    | 186   |   9000
总之,你会得到10 000。