Codeigniter - Как я могу сложить несколько столбцов - PullRequest
0 голосов
/ 07 ноября 2019

Я хочу суммировать и отобразить в таблице все ss_score_talent и ss_score_star, что равно значению ss_contestant_name.

Модель:

function get_average_score()
  {
    $this->db->select("event_contestant.cnt_id, event_contestant.cnt_fullname, event_score_sheet.ss_contestant_name, event_score_sheet.ss_date, event_score_sheet.ss_score_talent, event_score_sheet.ss_score_star, event_score_sheet_sc.sc_score_sc, event_score_sheet_pv.pv_score_pv");
    $this->db->from('event_contestant');
    $this->db->join('event_score_sheet', 'event_score_sheet.ss_contestant_name = event_contestant.cnt_fullname');
    $this->db->join('event_score_sheet_sc', 'event_score_sheet_sc.sc_contestant_name = event_contestant.cnt_fullname');
    $this->db->join('event_score_sheet_pv', 'event_score_sheet_pv.pv_contestant_name = event_contestant.cnt_fullname');
    $query = $this->db->get();
    return $query->result();
  }

Контроллер:

function read_averagescore()
  {
    $data = $this->Scoresheet_Model->get_average_score();
    echo json_encode($data);
  }

Вид: JS

function show_score_average()
  {
    $.ajax({
      type      : "GET",
      url       : "<?php echo base_url('scoresheet/read_averagescore');?>",
      async     : false,
      dataType  : "JSON",
      success   : function(data)
      {
        var html = '';
        var i;
        for(i=0; i<data.length; i++)
        {
          if (data[i].cnt_fullname == data[i].ss_contestant_name)
          {
            total_sc = math.sum(data[i].ss_score_talent, data[i].ss_score_talent, data[i].ss_score_talent);
          }
          var total_average = math.sum(data[i].ss_score_talent, data[i].ss_score_star, data[i].sc_score_sc, data[i].pv_score_pv);
          html +=
            '<tr>' +
              '<td>' + data[i].ss_date + '</td>' +
              '<td>' + data[i].cnt_fullname + '</td>' +
              '<td>' + math.sum(data[i].ss_score_talent,data[i].ss_score_talent,data[i].ss_score_talent) + '</td>' +
              '<td>' + data[i].ss_score_star + '</td>' +
              '<td>' + data[i].sc_score_sc + '</td>' +
              '<td>' + data[i].pv_score_pv + '</td>' +
              '<td>' + total_average + '</td>' +
            '</tr>';
        }
        $('#show_all_score_ca').html(html);
      }
    });
  }

см. Это выходное изображение

ОБНОВЛЕНИЕ: мой друг помог мне решить эту проблему. Вот запрос sql.

"SELECT `ss_contestant_name`, SUM(`ss_score_talent`) / COUNT(`ss_contestant_name`) AS totalTalent, SUM(`ss_score_star`) / COUNT(`ss_contestant_name`) AS totalStar, (SELECT `sc_score_sc` FROM `event_score_sheet_sc` WHERE `sc_contestant_name` = `ss_contestant_name`) AS totalSC, (SELECT `pv_score_pv` FROM `event_score_sheet_pv` WHERE `pv_contestant_name` = `ss_contestant_name`) AS totalPV FROM `event_score_sheet` GROUP BY `ss_contestant_name`"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...