创建显示学生花费多少的视图

时间:2018-11-25 10:51:45

标签: mysql sql view

我需要创建一个这样的视图:

document.getElementById("createNewList").addEventListener('submit', function(e) {
    e.preventDefault();

    // serialize form to convert into JSOn object
    let form_data = formSerialize(document.getElementById("createNewList"));

     // AJAX fetch()
    fetch('/', {
        method: 'POST',
        headers: {
            "Content-Type": "application/json; charset=utf-8",
            // "Content-Type": "application/x-www-form-urlencoded",
        },
        body: form_data
    })
    .then(function(response) {      
         if(response.ok)
            return response.json();
    })
    .then(function(user_json) {
        console.log(user_json);

        console.log(user_json);
        create_todays_todo_list(user_json);
    });
});

// add event listener of the to do list i.e. adding tasks deleting tasks, completed tasks
// document.getElementById("toDoList_form").addEventListener('submit', function(e) {
    // e.preventDefault();
    // console.log("clikeddddddddd");
    // // AJAX send added task to server
    // let list_item = document.getElementById("list_item").innerHTML;
    // let user = document.getElementById("user_name_heading").innerHTML;
    // let data = {"user": user, "list_item": list_item}
    // console.log(data);
         // AJAX fetch()
        //  fetch('/', {
        //     method: 'POST',
        //     headers: {
        //         "Content-Type": "application/json; charset=utf-8",
        //         // "Content-Type": "application/x-www-form-urlencoded",
        //     },
        //     body: data
        // })
        // .then(function(response) {
        //      if(response.ok)
        //         return response.json();
        // })
        // .then(function(retrievedItemsFromServer) {
        //     console.log(retrievedItemsFromServer);
        // });

    //
// });


/// functionssss
// serialize form function
function formSerialize(form) {
    var input = form.getElementsByTagName("input");
    var formData = {};
    for (var i = 0; i < input.length; i++) {
      formData[input[i].name] = input[i].value;
    }
    return formData = JSON.stringify(formData);
  }
function create_todays_todo_list(user_json) {
    // 1. hide the create list form  form
    document.getElementById("createNewList").style.display="none";

    // 2. display the to do list div
    document.getElementById("toDoList").style.display="flex";

    // 3. display the user's name and date as logged in on the page
    document.getElementById("user_name_heading").innerHTML = user_json.userName;
    document.getElementById("user_date_signup_heading").innerHTML = user_json.userDate;

}

我有一张表格STUDENT JANUARY FEBRUARY MARCH ........ DECEMBER miki 10.23 23.23 0 0 Goku 10 0 0 0 ,代表该学生在这个月和一年中的每笔花费。它包含一些学生所做的实际费用。

另一个表是studentMovement(id_studentmovement,id_student,month,year,cost,date),该表的所有月份的ID为Month(id_month)

(January==1 February==2 etc)

另一个视图是Students(id_student,name),其中包含一些年份。

我想得到一年,表中的所有学生(也是没有输出的学生)和整个月(如果这个月有输出,值为0),我想要得到所有费用的总和。所以我的看法是

Years(id_year)

问题是,如果一年中一个月没有花费,我需要输入0之类的值,但是对于此查询,它不会执行此操作。 问题是studentMovement具有实际输出。在上面的示例中,悟空只有1月份的费用,但是学生悟空没有参加2月的培训,等等,有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您似乎想要这样的东西:

select year(sm.date) as year, s.id_student, s.name,
       sum(amount) as year_amount,
       sum(case when month(sm.date) = 1 then amount else 0 end) as january,
       sum(case when month(sm.date) = 2 then amount else 0 end) as february,
       . . .
       sum(case when month(sm.date) = 12 then amount else 0 end) as december,
        "m"."id_month" AS "id_month",
        coalesce(sum("sm"."amount"),0) AS "amount",
from schema.students s join
     schema.studentMovement sm 
     on s.d_student = sm.id_student 
group by year(sm.date) as year, s.id_student, s.name;

. . .是剩余月份的逻辑。