When I perform a SELECT it returns multiple duplicate records

Asked

Viewed 471 times

-1

When I perform a SELECT it returns multiple table posts records with same id;

I tried to use DISTINCT not to return these records but it’s still returning duplicate records.

Imagem dos registros do retorno do select

SELECT DISTINCT * FROM tag_post 
INNER JOIN tag ON tag.id_tag = tag_post.id_tag 
WHERE tag.nome_tag LIKE '%quimica%';
  • But there are two records with the same id_postagem with nome_tag different. They are two different things. If you only want one, which one?

  • It’s a bit confusing your question. What is the result you would like to get? Because with Inner Join you get the related data of the two tables. In this case, as I understand it, you’re taking all the posts' tags.

  • In what you posted there are no fully duplicated lines. Explain better what kind of "duplicity" you want to avoid.

  • You’d have to send examples of the tables as well, not just the view. And a tip: usually those who use DISTINCT are already doing something wrong, DISTINCT is a band-aid and almost always have a better way.

2 answers

0

Hello

Try it this way:

SELECT DISTINCT tag_post.id_postagem, tag_post.*, tag.nome_tag FROM tag_post 
LEFT OUTER JOIN tag ON tag.id_tag = tag_post.id_tag 
WHERE tag.nome_tag LIKE '%quimica%';

Or so:

SELECT DISTINCT tag_post.*, tag.nome_tag FROM tag_post 
LEFT OUTER JOIN tag ON tag.id_tag = tag_post.id_tag 
WHERE tag.nome_tag LIKE '%quimica%';
  • My problem was not to appear repeated id_tag, but to have 2 records with equal id_post

0

I solved my problem, my teacher reminded me that as I was picking up *, the records returned were different because of the_tagcolumn, so the distinct would not work, to solve it was just not to take the_tagcolumn. As for what I needed just needed the post.id_post, it only returns single id records.

SELECT DISTINCT postagem.id_postagem FROM postagem
INNER JOIN tag_post on postagem.id_postagem = tag_post.id_postagem
INNER JOIN tag ON tag.id_tag = tag_post.id_tag 
WHERE tag.nome_tag LIKE '%quimica%';

inserir a descrição da imagem aqui

and without the DISTINCT

SELECT postagem.id_postagem FROM postagem
INNER JOIN tag_post on postagem.id_postagem = tag_post.id_postagem
INNER JOIN tag ON tag.id_tag = tag_post.id_tag 
WHERE tag.nome_tag LIKE '%quimica%';

inserir a descrição da imagem aqui

Browser other questions tagged

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