Get the repeated records from one column from another column

Asked

Viewed 260 times

0

Hello to all! I am facing the following problem: I want to get the amount of values that repeat in the column disease type from the column of locality, that is to say, take the amount of records with the same disease from a single locality inserir a descrição da imagem aqui

FROM NOW THANK YOU :D

  • No need to put solved on the question. The fact that you accept my answer is already understood as solved.

1 answer

1


Friendly:

SELECT localidade, tipo_doenca as doenca, COUNT(tipo_doenca) as quantidade 
FROM pessoasDoentes GROUP BY localidade, tipo_doenca

See working on SQL Fiddle

In php you just do this:

    $sql = "

    SELECT localidade, tipo_doenca as doenca, COUNT(tipo_doenca) as quantidade 
     FROM pessoasDoentes GROUP BY localidade, tipo_doenca

    ";

    $resultado = $conn->query($sql);

    // imprimir os nossos resultados
    while($row = $resultado->fetch(PDO::FETCH_OBJ)) {
        echo $row->localidade.' = '.$row->doenca.' -> '.$row->quantidade;
    }

If you have the locality, you can do this:

    $localidade = "sao paulo";
    $sql = "

    SELECT tipo_doenca as doenca, COUNT(tipo_doenca) as quantidade 
     FROM pessoasDoentes WHERE localidade = '$localidade' GROUP BY  tipo_doenca

    ";

see working

Note: I put the table name as "personal", because you did not put the original name in the question. Then change to the correct name.

  • 1

    Thank you! You saved my life and my T.C.C.

  • @Kingbrdzn glad I could help! Good luck! =)

Browser other questions tagged

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