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

303
Visualizações
How do structure this query to combine two tables based on conditions

I am brand new here, I read a lot but i didn't have an account until now, I wonder if someone would be kind enough to help me?

I have three tables: Tasks Users Notes

Tasks and notes have a foreign key 'id' to the user table. Notes has the foreign key 'id' from the task table.

These are the queries separately:

SELECT * FROM notes WHERE taskid='id'

SELECT id, userid, taskid, nickname FROM notes WHERE taskid='id'
AND NOT userid='id' 

SELECT * FROM notes WHERE taskid='id' AND userid='id

What I want to do is combine this query into one, so that I end up with Tasks and offers that match the queries above in the same table.

I tried to inner join and union all but each select statement has a different number of columns, so I am a little stuck.

What I want to end up with is all tasks plus all notes for that task where the user is the owner of the task and the note. Plus all notes for the task where the user is not the owner(only a subset of the notes info) not all fields.

Thanks in advance, if you need more info ask.

Ok here is a spread sheet with a simple example of the result:

https://docs.google.com/spreadsheets/d/1UEPCyMLGKqd0ylafUvUVquUxcZ43V3sQFKYO3SjwIGo/edit?usp=sharing

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

How about doing just SELECT * FROM notes WHERE taskid='id' order by userid and then doing the rest of processing in your programming language?

over 4 years ago · Santiago Trujillo Relatório

0

I found the answer anyway, it was so simple, another guy suggested I should just null the fields of one of the tables in the query Simple but now the queries work!

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