如何根据名称放置单元格值?

时间:2017-10-06 14:33:10

标签: php mysql

我想制作一张这样的表:

name  | Math | Physics | History | Biology | Gym
-------------------------------------------------------
Johnat|   5  |    3    |         |         |     
Sarah |   1  |         |  2      |  3      | 2

我有一个像这样的SQL结构:

艾内

id  |     nimetus   | 
-----------------
1   |   Math     |
2   |   Physics  |   
3   |   History  |   
4   |   Biology  |
5   |   Gym      |

学生

id  |     name   | 
-----------------
1   |   Johnat   |
2   |   Sarah    |   

Aine_student

id  |     aine_id | student_id   |
---------------------------------
1   |   1         |   1          |
2   |   2         |   1          |
3   |   1         |   2          |
4   |   3         |   2          |
5   |   4         |   2          |
6   |   5         |   2          |

Hinne

id  |     tulemus | aine_tudeng_id|
---------------------------------
1   |   5         |   1          |
2   |   3         |   2          |
3   |   1         |   3          |
4   |   2         |   4          |
5   |   3         |   5          |
6   |   2         |   6          |

现在我做了一个像这个女巫包含我需要的所有数据的查询

SELECT t.name,a.nimetus,h.tulemus FROM aine_student at
                                  INNER JOIN student t ON t.student_id=at.student_id
                                  INNER JOIN aine a ON a.aine_id=at.aine_id
                  INNER JOIN hinne h ON h.aine_student_id=at.aine_student_id

查询结果

  name  |   nimetus   | tulemus|
---------------------------------
Johnat  |    Math      |   5   |
Sarah   |    Math      |   1   |
Johnat  |    Physics   |   3   |
Sarah   |    History   |   2   |
Sarah   |    Biology   |   3   |
Sarah   |     Gym      |   2   |

但是我觉得这个表的查询不正确,所以我为单独的aine and name,tulemus

创建了函数
  function getNimetus()
  {
    $query = $this->db->prepare("SELECT distinct a.nimetus FROM aine_student at
                                  INNER JOIN student t ON t.student_id=at.student_id
                                  INNER JOIN aine a ON a.aine_id=at.aine_id
                  INNER JOIN hinne h ON h.aine_student_id=at.aine_student_id ");
      $query->execute();
      $exist = $query->fetchAll();


      if(!$exist) {
           throw new Exception('DB error');  //you can send your exception
        }

        $nimetus=[];

        for ($i=0; $i < count($exist); $i++) {
          $nimetus[]=$exist[$i]["nimetus"];
        }

        return $nimetus;
  }



  function getTulemus()
  {
    $query = $this->db->prepare("SELECT t.name,h.tulemus FROM aine_student at
                                  INNER JOIN student t ON t.student_id=at.student_id
                                  INNER JOIN aine a ON a.aine_id=at.aine_id
                  INNER JOIN hinne h ON h.aine_student_id=at.aine_student_id ");
      $query->execute();
      $exist = $query->fetchAll();


      if(!$exist) {
           throw new Exception('DB error');  //you can send your exception
        }

        $tulemus=[];

        for ($i=0; $i < count($exist); $i++) {
          $tulemus[]=["Nimi"=>[$exist[$i]["nimi"]=>$exist[$i]["tulemus"]]];
        }

        return $tulemus;
  }


function createTable() {
  $nimi=$this->getNames();
  $nimetus=$this->getNimetus();
  $tulemus=$this->getTulemus();
  echo "<table><thead><th>Nimi</th>";
  for ($i=0; $i <count($nimetus) ; $i++) {
    echo "<th>".$nimetus[$i]."</th>";
  }
  echo "</thead><tbody>";
  echo "<pre>".print_r($tulemus,true)."</pre>";
  for ($i=0; $i <count($tulemus) ; $i++) {
    foreach ($tulemus[$i]["Nimi"] as $key => $value) {
      if($key==)
    }
  }
}

但这是我走了多远。我真的有问题制作这个表,所以任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:2)

此过程称为枢轴。 这是通过GROUP BY结合MAX和CASE

完成的

<强>查询

SELECT
   student.name 
 , MAX(
     CASE
       WHEN Aine.nimetus = 'Math'
       THEN Hinne.tulemus
       ELSE ''   
     END
   ) 
    AS Math
 , MAX(
     CASE
       WHEN Aine.nimetus = 'Physics'
       THEN Hinne.tulemus
       ELSE ''   
     END
   ) 
    AS Physics    
 , MAX(
     CASE
       WHEN Aine.nimetus = 'History'
       THEN Hinne.tulemus
       ELSE ''   
     END
   ) 
    AS History   
 , MAX(
     CASE
       WHEN Aine.nimetus = 'Biology'
       THEN Hinne.tulemus
       ELSE ''   
     END
   ) 
    AS Biology          
 , MAX(
     CASE
       WHEN Aine.nimetus = 'Gym'
       THEN Hinne.tulemus
       ELSE ''   
     END
   ) 
    AS Gym  

FROM 
 student

INNER JOIN
 Aine_student
ON
 student.id =  Aine_student.student_id

INNER JOIN 
 Aine
ON
  Aine.id = Aine_student.aine_id 

INNER JOIN 
 Hinne
ON 
 Aine.id = Hinne.aine_student_id 

GROUP BY
 student.name 

<强>结果

我的结果与您的预期结果不同。 我认为在制作例外结果表时你犯了一些错误。

|   name | Math | Physics | History | Biology | Gym |
|--------|------|---------|---------|---------|-----|
| Johnat |    5 |       3 |         |         |     |
|  Sarah |    5 |         |       1 |       2 |   3 |

demo http://www.sqlfiddle.com/#!9/62aa4a/1