Mysql,3表2组

时间:2014-01-03 02:57:48

标签: mysql sql

我有3张桌子,学生,家人和费用。有些家庭有一个以上的孩子。费用记录在费用表中。家庭在这一年里付了很多钱。 我想按家庭分组孩子的数量,并获得每个家庭支付的总金额。 我的Qyery:

SELECT student.Family_ID, family.Family_Name, count(*) as kids_numb, sum (Amount)  as Paid_Amount        
      FROM student, family, fee 
      WHERE student.Family_ID = family.Family_ID
      AND fee.Family_ID = family.Family_ID
      AND student.Status ='1'
      GROUP BY student.Family_ID;

我需要对以下内容进行排序:

Family Name  |  # of kids |  Fees  | Paid  | Balance
----------------------------------------------------
Lebon        |      1     |  425   | 200   |  225
Lavoix       |      2     |  700   | 150   |  550
Napper       |      1     |  425   |  0    |  425
Major        |      3     |  900   | 300   |  600

1 个答案:

答案 0 :(得分:2)

根据您的评论

更新

以这种方式试试

SELECT m.family_id, m.family_name, s.kids_numb, COALESCE(f.paid_amount, 0) paid_amount
  FROM family m LEFT JOIN
(
  SELECT family_id, COUNT(*) kids_numb
    FROM student
   WHERE status = 1
   GROUP BY family_id
 ) s 
    ON m.family_id = s.family_id LEFT JOIN 
(
  SELECT family_id, SUM(amount) paid_amount
    FROM fee 
    GROUP BY family_id
) f
  ON m.family_id = f.family_id
 WHERE s.family_id IS NOT NULL

示例输出:

| FAMILY_ID | FAMILY_NAME | KIDS_NUMB | PAID_AMOUNT |
|-----------|-------------|-----------|-------------|
|         1 |       Lebon |         1 |         200 |
|         2 |      Lavoix |         2 |         150 |
|         3 |      Napper |         1 |           0 |
|         4 |       Major |         3 |         300 |

这是 SQLFiddle 演示


UPDATE2:添加总计

SELECT family_id, family_name, SUM(kids_numb) kids_numb, SUM(paid_amount) paid_amount
  FROM
(
  SELECT m.family_id, m.family_name, s.kids_numb, COALESCE(f.paid_amount, 0) paid_amount
    FROM family m LEFT JOIN
  (
    SELECT family_id, COUNT(*) kids_numb
      FROM student
     WHERE status = 1
     GROUP BY family_id
   ) s 
      ON m.family_id = s.family_id LEFT JOIN 
  (
    SELECT family_id, SUM(amount) paid_amount
      FROM fee 
      GROUP BY family_id
  ) f
    ON m.family_id = f.family_id
   WHERE s.family_id IS NOT NULL
) q
 GROUP BY family_id, family_name WITH ROLLUP
HAVING (family_id IS NOT NULL AND 
        family_name IS NOT NULL)
    OR (family_id IS NULL AND 
        family_name IS  NULL)

示例输出:

| FAMILY_ID | FAMILY_NAME | KIDS_NUMB | PAID_AMOUNT |
|-----------|-------------|-----------|-------------|
|         1 |       Lebon |         1 |         200 |
|         2 |      Lavoix |         2 |         150 |
|         3 |      Napper |         1 |           0 |
|         4 |       Major |         3 |         300 |
|    (null) |      (null) |         7 |         650 |

MySQL GROUP BY非标准扩展程序将允许您更改整个部分

 GROUP BY family_id, family_name WITH ROLLUP
HAVING (family_id IS NOT NULL AND 
        family_name IS NOT NULL)
    OR (family_id IS NULL AND 
        family_name IS  NULL)

只是

 GROUP BY family_id WITH ROLLUP

但是,在NULL列的family_name代替{{1}}总行,您将拥有最后一个姓氏的值。

示例输出:

| FAMILY_ID | FAMILY_NAME | KIDS_NUMB | PAID_AMOUNT |
|-----------|-------------|-----------|-------------|
|         1 |       Lebon |         1 |         200 |
|         2 |      Lavoix |         2 |         150 |
|         3 |      Napper |         1 |           0 |
|         4 |       Major |         3 |         300 |
|    (null) |       Major |         7 |         650 |

这是 SQLFiddle 演示