我有一个返回此结果的SQL查询:
但我想像那样显示它
我如何在PHP中做到这一点,我尝试了但是我发现了一些有关'Semaine X'的问题,它将成为表Column的名称和'libelleCategorieClient'的值,它不能重复 谢谢,
答案 0 :(得分:1)
为了将行转换为列(Pivoting),假设libelleCategorieClient是唯一的,请尝试以下操作:
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', 'pass');
$q = $db->query("SELECT DISTINCT cat.libelleCategorieClient,
sum(ligne.quantiteLigneCommande * ligne.prixLigneCommande) AS montant,
sem.libelleSemaine
FROM CategorieClient cat,Client c, commande cmd,
lignecommande ligne,semaine sem
WHERE cat.idCategorieClient=c.idCategorieClient
AND c.idClient=cmd.idClient AND cmd.idCommande=ligne.idCommande
AND sem.idSemaine=cmd.idSemaine
GROUP BY cat.libelleCategorieClient, sem.libelleSemaine");
$sem=array();
$client=array();
while($row=$q->fetch(PDO::FETCH_ASSOC)){
$sem[$row['cat.libelleCategorieClient']][$row['libelleSemaine']] = $row['montant'];
$client[$row['cat.libelleCategorieClient']]= $row['libelleCategorieClient'];
}
$semkey=key($sem);
echo '<table>';
echo '<tr>';
echo '<th>Cetegories Clients</th>';
foreach($sem[$semkey] as $keys=>$vals){
echo '<th>'.$keys.'</th>';
}
echo '</tr>';
foreach($sem as $key=>$val){
echo '<tr>';
echo '<td>'.$client[$key].'</td>';
foreach($val as $v){
echo '<td>'.$v.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>