gpt4 book ai didi

php - mysql 查询获取一些表/数组

转载 作者:行者123 更新时间:2023-11-29 11:36:50 25 4
gpt4 key购买 nike

我有一些像这样的表格分数...

id A  B  score
1 10 4 100
2 10 2 320
3 10 1 100
4 20 4 20
5 20 3 100
6 20 2 120
7 20 1 110
8 30 4 30
9 30 3 200

我想要一些查询或 php 方法来生成类似的输出

column A =>    10    20     30
4 = 100 20 30
3 = null 100 200
2 = 320 120 null
1 = 100 110 null
^
||
(coumn b)

         4 ,100,20,30
3 ,null,100,200
2 ,320,120,null
1 ,100,110,null

所以我想获得列分数,但基于列 B,如果 A 列没有 B 列的行分数,则会给出 null。

我尝试过完全连接、交叉连接等,但我没能得到这样的结果。

  SELECT * FROM TABLE CROSS JOIN
(SELECT * FROM TABLE CROSS GROUP BY B)
ORDER BY B DESC

谢谢

最佳答案

这是另一种执行嵌套循环的方法(使用 mysqli_*):

echo '<table>';

$stmt = $connection->prepare("SELECT B FROM TABLE ORDER BY B DESC");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($b);
while($stmt->fetch()){

echo '<tr>
<td>'.$b.'</td>';

$stmt2 = $connection->prepare("SELECT A FROM TABLE GROUP BY A ORDER BY A");
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($a);
while($stmt2->fetch)){

$stmt3 = $connection->prepare("SELECT score FROM TABLE WHERE A = ? AND B = ?");
$stmt3->bind_param("ii", $a, $b);
$stmt3->execute();
$stmt3->store_result();
if($stmt3->num_rows > 0){
$stmt3->bind_result($score);
$stmt3->fetch();
echo '<td>'.$score.'</td>';
} else {
echo '<td>NULL</td>';
}
$stmt3->close();

} /* END OF WHILE LOOP OF SECOND STATEMENT */
$stmt2->close();

echo '</tr>'; /* END OF ROW */

} /* END OF WHILE LOOP */
$stmt->close();

echo '</table>';

但我很确定有人会通过一个查询回答您的问题。

关于php - mysql 查询获取一些表/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36440275/

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