Mysql Left Outer Join show only first result

Asked

Viewed 105 times

0

I need to make this sales chart Join with the Nfe table, however the NFE table may have more than one result which causes change in total sales, how can I change this Join so that only take the last note?

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) 
  • I updated the answer according to your need.

1 answer

2

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

To get just the last note, remove the SUM and give an order by DESC by the cod_sale 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
  • In case I would need you to enter the sum only one note, as it may occur of the same sale to have more than one note

  • So it’s not '(...) only the last note'. For what you asked @Victormangia, this answer answers. If you clarify your question maybe the AR or someone else can help you better.

  • @Victormangia swaps v.cod_sell for the note number field of your note table, and add limit 1 as an example. You must solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.