Mysql Left outer join show only the first result

0

I need to make this join of the sales table with the Nfe table, but the NFE table can have more than one result which causes a change in total sales, how can I change this join to get only the last note? p>

select 
sum(total_venda) as total_venda
from vendas v 
LEFT OUTER JOIN nfe n ON (v.cod_venda = n.cod_venda and n.status like 'Emitida' and n.excluido is null) 
    
asked by anonymous 03.07.2018 / 14:43

1 answer

2

In fact, it does not show only the first result. It is showing what you are asking for (the sum of the total_sales column). Being a sum, the value is grouped, so it will not show more than one result.

To get only the last note, remove the SUM and give an order by DESC by the cod_venda or if you have a date for it. It would look something like

select 
total_venda as total_venda
from vendas v 
LEFT OUTER JOIN nfe n ON (v.cod_venda = n.cod_venda and n.status like 'Emitida' and n.excluido is null)
Order by v.cod_venda desc limit 1
    
03.07.2018 / 14:46