gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:59:03 27 4
gpt4 key购买 nike

我想做一个这样的表:

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 |

欣内

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 和 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==)
}
}
}

但这就是我的进展。我在制作这张 table 时确实遇到了问题,因此非常感谢您的帮助。

最佳答案

这个过程称为枢轴。这是通过 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

结果

我的结果与您的预期结果不同。我认为您在制作异常(exception)结果表时犯了一些错误。

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

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

关于php - 如何根据名称放置单元格值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com