简体   繁体   中英

Show one row only in same id

I would like to ask how to show one row only in same id, in below example, I just want to show the highest score result in the table.

Here is my code

    $query = "SELECT * FROM memberdata ORDER BY score DESC";
    echo "<table border=1>
    <tr>
    <th> id </th>
    <th> score </th>

    </tr>";

    while($row = mysqli_fetch_array($result)) {

        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['score'] . "</td>";
        echo "</tr>";
    }

    echo "</table>";

And the Output is

id score 
1  5000
1  4000
1  3000
1  500
2  3000
2  1000

使用分组依据

SELECT id, MAX(score) AS score FROM memberdata GROUP BY id

Try this:

SELECT id, MAX(score) AS score FROM memberdata GROUP BY id

or this:

SELECT * FROM memberdata ORDER BY score DESC LIMIT 1

You need to iterate to find the maxrow first.

    $query = "SELECT * FROM memberdata ORDER BY score DESC";
    echo "<table border=1>
    <tr>
    <th> id </th>
    <th> score </th>

    </tr>";

    $maxrow = mysqli_fetch_array($result);
    while($row = mysqli_fetch_array($result)) {
        if ($maxrow['score'] > row['score']) maxrow = row;
    }
    echo "<tr>";
    echo "<td>" . $maxrow['id'] . "</td>";
    echo "<td>" . $maxrow['score'] . "</td>";
    echo "</tr>";
    echo "</table>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM