sql 分组依据 - 显示所有行

sql group by - display all rows

我正在为一些朋友开发一些基本的统计数据跟踪网站,我想稍微组织一下各个球员的页面。

我的计划是根据他们玩的日期打印出单独的表格,这样他们就可以更轻松地查看特定日期的游戏。

我是 php 的新手,我发现了 "GROUP BY",虽然这可以按日期正确分组,但它只有 returns 一行...有没有办法获得每个"GROUP BY?"

内的行

这是我现在拥有的:

$query = "SELECT * FROM games WHERE player='$thePlayer' GROUP BY whenPlayed";

尝试这种方式首先 ORDER 您的行 whenPlayed 升序或降序,如果您需要单独日期或其他内容的行槽,请添加 where 条件。

$query = "SELECT * FROM games WHERE player='$thePlayer' ORDER BY whenPlayed ASC";
// Get data
$query = "SELECT * FROM games WHERE player = '$thePlayer' ORDER BY whenPlayed";
$result = $mysqli->query($query);


$data = array();
// Iterate over data, use 'whenPlayed` as a key to group your data
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    if (!array_key_exists($row['whenPlayed'], $data)) {
        $data[$row['whenPlayed']] = array();
    }
    $data[$row['whenPlayed']][] = $row;
}

然后将其显示在列表中,例如:

<ul>
    <?php foreach ($data as $date => $games): ?>
    <li>
        <?php echo $date ?>:
        <ul>
            <?php foreach ($games as $game): ?>
            <li><?php echo print_r($game, true) ?></li>
            <?php endforeach ?>
        </ul>
    </li>
    <?php endforeach ?>
</ul>