计算具有三个表的新列

时间:2016-11-19 16:39:09

标签: mysql jointable

我有 3个输入表

    **Student Table**     
    -------------
    id    name
    -------------
    201     shiva
    202     Jitendra
    203     Makarand
    204     Arpit

    **Position Table**
    -------------
    id    rank
    -------------
    203    1
    201    2
    202    3
    204    4

    **House Table**
    ------------------
    housename    id
    ------------------
    Yellow        201
    Blue          202
    Red           203
    Yellow        204

学生表中有n个记录

我想用公式P = sum(n + 1 - rank)* 100

计算点(列)
    ** Output Table **
-------------------------------
HouseName      Name      Points
-------------------------------
yellow         Shiva         300
Red            Makarand      200
Yellow         Arpit         100

我写了这个查询:

select h.housename "House Name",
       s.name "Name",
       (sum ( (count(*) from s + 1) - p.rank ) ) * 100 as "Points"
       from House h,Student s,Position p
       where h.housename = (select s.name where s.id = h.id)
       order by Points

示例SQL小提琴在这里:http://sqlfiddle.com/#!9/4d823/26

我是SQL的新手,我怎样才能正确查询此查询?

1 个答案:

答案 0 :(得分:0)

如果你想从三个表中获得积分(使用公式),你的问题就不清楚了,

 select h.housename "House Name",
   s.name "Name",
   ((count(s.id) + 1 - p.rank ) * 100) as "Points"
   from House h join Student s on s.id = h.id join Position p on p.id=s.id order by Points

我没有运行此查询。你能检查一下吗?