Duplicate result in Mysql in while

Asked

Viewed 109 times

0

The table has two columns, one of user data and another of user points, the user points works like the bank statement scheme, but need to return only the last value, and I did so

SELECT `usuario`.*, `pontos`.`ponto_valor` FROM `usuario` INNER JOIN `pontos` ON `pontos`.`ponto_id_user` = `usuario`.`user_id` ORDER BY `usuario`.`data_cad` DESC, `pontos`.`data_cad` DESC;

And I did a while with PHP for that:

<?php
$instrucaoSQL = "SELECT 
                `usuario`.*, 
                `pontos`.`ponto_valor` 
            FROM 
                `usuario` 
            INNER JOIN 
                `pontos` 
            ON 
                `pontos`.`ponto_id_user` = `usuario`.`user_id` 
            ORDER BY 
                `usuario`.`data_cad` 
                    DESC,
                `pontos`.`data_cad`
                    DESC;";

$instrucaoQuery = mysqli_query($conn, $instrucaoSQL);

$contaQuery = mysqli_num_rows($instrucaoQuery);

if($contaQuery > 0) {

    while ( $r = mysqli_fetch_array($instrucaoQuery) ) {

        echo $r['user_nome'] . " - " . $r['ponto_valor'] . "<br>";

    }
?>

However the result comes all duplicated, I would like only the last value (row) of the table 'points' to be displayed, without duplicating the names of the table 'user' every time I find a related value in the table 'points'.

  • That $r is what?

  • is array, which receives database results, but solved, thanks.

1 answer

0

I solved my SQL like this:

$instrucaoSQL = "
            SELECT 
                `usuario`.*,
                `pontos`.`ponto_valor`
            FROM 
                `usuario` 
            INNER JOIN
                `pontos`
            ON
                `pontos`.`ponto_id_user` = `usuario`.`user_id`
               WHERE
                (
                SELECT 
                    `ponto_valor`
                ORDER BY
                    `pontos`.`data_cad`
                        DESC
                LIMIT 1
                )
             GROUP BY
                `usuario`.`user_nome`
                        ";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.