来自MySQL的数据--如果用户有相同数量的积分--显示在一行。

问题描述 投票:0回答:1

如何使如果玩家X的点数与其他玩家相同,使其显示为1行?

表用户。

`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(120) NOT NULL DEFAULT '',
`points_first` int(11) NOT NULL DEFAULT 0,
`points_second` int(11) NOT NULL DEFAULT 0,

我的查询:

SELECT uid, username, points_first, points_second
FROM users
LIMIT 5

结果:

enter image description here

PHP对数据进行DESC排序并使用模板显示。

$query = $db->query('SELECT uid, username, points_first, points_second FROM users');
        $no = $noFirst = $noSecond = 0;
        $dataFirstPoints = $dataSecondPoints = [];
        $i = 0;
        while($row = $db->fetch_array($query)){
            if($row['points_first'] != 0){
                $dataFirstPoints[$i]['points'] = $row['points_first'];
                $dataFirstPoints[$i]['username'] = $row['username'];
            }
            if($row['points_second'] != 0){
                $dataSecondPoints[$i]['points'] = $row['points_second'];
                $dataSecondPoints[$i]['username'] = $row['username'];
            }
            $i++;
            if($row['points_second'] === 0) $firstRows = "<div class=\"nodata\">No Data</div>";
            if($row['points_first'] === 0) $secondRows = "<div class=\"nodata\">No Data</div>";
        }
        arsort($dataFirstPoints);
        arsort($dataSecondPoints);
        foreach($dataFirstPoints as $data){
            $points = $data['points'];
            $noFirst++;
            $no = $noFirst;
            eval("\$secondRows .= \"".$templates->get("rows")."\";");
        }
        foreach($dataSecondPoints as $key => $data){
            $points = $data['points'];
            $noSecond++;
            $no = $noSecond;
            eval("\$firstRows .= \"".$templates->get("rows")."\";");
        }
        eval("\$main = \"".$templates->get("main")."\";");

结果来自PHP和模板。

enter image description hereenter image description here

现在我们可以看到两个用户的积分在同一个类别中(points_first).如何才能得到这个效果?用什么来实现?

enter image description here

php mysql
1个回答
1
投票

将结果按点进行分组,并使用GROUP_CONCAT来连接用户名。

SELECT points_first, GROUP_CONCAT(username SEPARATOR ', ') AS users FROM users GROUP BY points_first ORDER BY points_first DESC;

希望对大家有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.