Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Find the rows that has the same column

I want to know how to do the following in SQL :

SELECT * 
FROM table_A 
WHERE id IN(:myValues)
AND other_colum has the same value

For example, if i've a conversation table(iduser,idconversation), I want SQL query that returns some of Ids that have the same conversation id. It should return

35;105
37;105
35;106
37;106

With 35,37 the idUsers and 105,106 the conversations they have in common.

To go further, i work with Doctrine and PostegreSQL, and the table that I want to query is generated (many to many relation) but i've difficulty to integrate sub-query.

  **public function getAllCommonConversationByUserId($ids)
  {
      return $this->createQueryBuilder('c')
          ->select('c.id')
          ->innerJoin('c.idUser', 'recievedConversation')
          ->where('recievedConversation IN (:ids)')
          ->andWhere('$qb->expr()->eq("SELECT id FROM table GROUP BY(id) HAVING COUNT(*) >1")')
          ->setParameter(':ids', $ids)
          ->getQuery()
          ->getResult();
  }**
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Just:

SELECT * 
FROM table_A 
WHERE idconversation in ('105','106') and iduser in ('35','37')

UPDATE: Are you saying if the idconversation is duplicate? (showing multiple times?)

If so:

Select * 
From table 
where idconversation in 
(
Select idconversation 
From table
group by (idconversation)
Having count(*) >1
)
--where iduser in ('35','37')
over 4 years ago · Santiago Trujillo Relatório

0

You can get the conversations using group by and having:

SELECT conversationid
FROM table_A 
WHERE userid in (35, 37)
GROUP BY userid
HAVING count(distinct userid) = 2;

If you want the original rows, you can join back to the original table.

over 4 years ago · Santiago Trujillo Relatório

0

Try to do this:

select id, conversation
from [your table name]
where 
    conversation in (
        select conversation 
        from [your table name]
        where id in (35)
    )

It return all of the participants of the conversation with user id = 35

If you have duplicates in your table, please add distinct to select statement.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda