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

231
Visualizações
How do I return all results of an inner join when filtering by an attribute of a joined table

I have a SQL query that filters logs by what kind of tags they have. Sometimes the logs have multiple tags. The issue is that when I filter by a tag, the returned logs only include the tag that I've filtered with. When filtering by a tag, I would like to get all logs with that tag, with all of their tags.

i.e. When filtering by "a pink tag" I would like to have... enter image description here

But instead I'm getting this... enter image description here

Below is my SQL query

        var logsQuery = `PREPARE LogQuery (text) AS\
            SELECT trackerlogs.*,\
            trackers.serialnumber,\
            trackers.active,\
            trackers.actdate,\
            trackers.deactdate,\
            trackerclaims.devicename,\
            trackerclaims.claimtype,\
            t.id AS tagid,\
            t.tagname,\
            t.color\
            FROM trackerlogs INNER JOIN\
            trackers ON trackerlogs.deviceid = trackers.id\
            INNER JOIN trackerclaims ON trackerclaims.deviceid = trackers.id\
            LEFT JOIN\
            (SELECT tags.*, trlogtags.logid AS logid from tags\
            INNER JOIN trlogtags ON trlogtags.tagid = tags.id\
            ${tags.length > 0 ? `AND t.id IN (${tags.join()})` : ""}\
            EXECUTE LogQuery('${search === "" ? "%" : "%" + search + "%"}');`;

I believe the relevant part of the query is:

${tags.length > 0 ? `AND t.id IN (${tags.join()})` : ""}\

However every time I try to change this, I'm not able to filter by tags anymore.

Any help is greatly appreciated. Please let me know if the explanation isn't clear, additional context is needed, or if the answer is somewhere else on StackOverflow and I'm blind.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Maybe you'll need to this in 2 steps:

  1. Get all the logs that have the tag that you'll need
  2. Join all tags from the logs from step 1

Something like this:

with selected_logs as (
    select
        l.id as log_id
    from logs l
    join tags t on t.log_id = l.id
        and t.tag_name = '{{filter}}'
)
select 
    l.*,
    t.*
from selected_logs sl
    join logs l on sl.log_id = l.id
    join tags t on t.log_id = sl.log_id


about 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