Repeated values when making an INNER JOIN

Asked

Viewed 1,214 times

0

This code below is bringing repeated values on the screen.

Could someone help me ?

$id_credenciado = $_SESSION['id_credenciado'];    

//$sql = mysqli_query($conn, "SELECT DISTINCT * FROM tb_protocolo INNER JOIN tb_certidao ON tb_protocolo.id_credenciado = tb_certidao.id_credenciado WHERE tb_protocolo.id_credenciado = $id_credenciado AND tb_certidao.id_credenciado = $id_credenciado");

$sql = mysqli_query($conn, "SELECT DISTINCT * FROM tb_protocolo INNER JOIN tb_certidao WHERE tb_protocolo.id_credenciado = $id_credenciado AND tb_certidao.id_credenciado = $id_credenciado");
  • This code is bringing repeated values on the screen.... Could someone help me ?

  • How are your table structures?

  • 1

    If you use * in the query will bring all of the two tables, and it is absolutely ok to bring duplicated data if it is not a relation 1:1. See that some field should be different, not everything will be 100% equal. DISTINCT will not solve the problem with * because any different field configures a different line

2 answers

1

In fact it is not bringing repeated values.

What happens is that the distinct compares all columns.

Just one column in the record is different that it will already separate into two records.

To solve this you need to specify only the search columns you need, instead of bringing them all using the asterisk (*).

0

Test do so:

$sql = mysqli_query($conn, 
"SELECT DISTINCT p.* FROM tb_protocolo p INNER JOIN tb_certidao c ON c.id_credenciado = p.id_credenciado WHERE c.id_credenciado = $id_credenciado");

We are joining the certificate table in the query by linking to the main code of the first table.

Browser other questions tagged

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