douwen5924 2016-05-01 15:22
浏览 35
已采纳

MySQL使用sum获取表

I am trying to get a table out of three different tables. I managed to count the paid field but cant get the correct values for Fee.

The Customer Table
    ╔════╦═════════════╗
    ║ ID ║    NAME     ║
    ╠════╬═════════════╣
    ║  1 ║ John Smith  ║
    ║  2 ║ Jim Jimmers ║
    ╚════╩═════════════╝
    The Event table
    ╔════╦═════════════╦═════════════╦═════════════╗
    ║ ID ║    Event    ║ Decription  ║    Fee      ║
    ╠════╬═════════════╬═════════════╬═════════════╣
    ║  1 ║ Rock Music  ║ Interseting ║          80 ║
    ║  2 ║ Pop music   ║ Interseting ║          90 ║
    ╚════╩═════════════╩═════════════╩═════════════╝


    The Payment table
    ╔════╦═════════════╦═════════════╦═════════════╗
    ║ ID ║already Paid ║ Customer_ID ║ Event_ID    ║
    ╠════╬═════════════╬═════════════╬═════════════╣
    ║  1 ║  0          ║           1 ║          1  ║
    ║  2 ║ 90          ║           1 ║          2  ║
    ║    ║             ║             ║             ║
    ║  3 ║ 90          ║           2 ║          2  ║
    ║  4 ║ 80          ║           2 ║          1  ║
    ╚════╩═════════════╩═════════════╩═════════════╝

i am trying to get this table paid refers to what he already paid and fee refers to the events´ that he has to pay

    ╔═══════╦══════════╦═══════╗
    ║Name   ║ paid     ║ Fee   ║
    ╠═══════╬══════════╬═══════╣
    ║John Sm║       90 ║  170  ║
    ║Jim Jim║      170 ║  170  ║
    ║       ║          ║       ║
    ║       ║          ║       ║
    ║       ║          ║       ║
    ║       ║          ║       ║
    ╚═══════╩══════════╩═══════╝
  • 写回答

1条回答 默认 最新

  • dsdsm2016 2016-05-01 15:30
    关注

    untested:

    select c.name, sum(p.paid), sum(e.fee)
      from customer c
      join payment  p on c.id       = p.customer_id
      join event    e on p.event_id = e.id
    group by c.name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类