mysql duplicating result from select

-2

Good afternoon guys, I'm new to the forum and I have a problem, I think it's simple in sql, it duplicates the result, can anyone help me?

SELECT DISTINCT o.id, s.id, s.servico, s.descricao, s.valor, s.orcamento_id, p.id, p.nome, p.quantidade, p.valor_unitario, p.valor_total, p.orcamento_id FROM servico AS s INNER JOIN pecas AS p INNER JOIN orcamento AS o WHERE o.id = 2 AND s.orcamento_id = o.id AND p.orcamento_id = o.id

    
asked by anonymous 04.12.2018 / 22:44

1 answer

-1

Wesley, how's my young man doing?

Well, come on, I took a look at your instruction here and I think the code below will help you. Try to run it and come back here to give the answer to people.

SELECT DISTINCT 
    o.id    as orcamento_id, 
    s.id    as servico_id, 
    s.servico, 
    s.descricao, 
    s.valor, 
    p.id    as pecas_id, 
    p.nome, 
    p.quantidade, 
    p.valor_unitario, 
    p.valor_total, 

FROM 
    servico AS s 
    INNER JOIN orcamento AS o ON (s.orcamento_id = o.id)
    INNER JOIN pecas AS p on (p.orcamento_id = o.id)
WHERE 
    o.id = 2 

Now let's get my changes:

  • Whenever you have columns with equal names such as: o.id, s.id, p.id, try to put an alias (surname) more organized like this.
  • I took your links from where and passed them to ON shortly after each joins.
  • I removed the columns for the _orcamento_id and s.orcamento_id because it was repeated too.
  • Finally, use code formatting when posting your questions. gets mis organized and helps who will read.
  • Good luck, I await your return.

        
    04.12.2018 / 23:20