How to order a group in the mysql query?

Asked

Viewed 482 times

1

I’m trying to order a query in the database grouping the information, but this showing the first registered in the database, but I want the last.

SELECT *
FROM base_rating AS B
JOIN historico_rating AS H ON B.id = H.id_base_rating
JOIN escala_rating AS E ON E.id = H.id_rating_atual
WHERE H.titulo!=''
  AND E.agencia!=''
GROUP BY B.id_emissao
  • Which fields do you want to return? Ordered by which field?

  • If you want to bring the last just, do one order by coluna DESC limit 1

  • I want the last of each group (the most recent of each ) not the last general

1 answer

1


Although I think your SQL does not run, for your need, does B.id_emissao desc.

SQL full

select 
  * 
from
  base_rating as B 
  JOIN historico_rating as H on B.id = H.id_base_rating 
  JOIN escala_rating as E on E.id = H.id_rating_atual 
where 
  H.titulo!='' and 
  E.agencia!='' 
group by 
  B.id_emissao desc

Edited

If you only want 1 record do as indicated by other users, add limit 1

  • As he said he wanted the last record only, he would have to have the limit 1, right?

  • @R.Santos edited response and added its consideration

  • This code shows the first registrants I wanted the last of each group, ie the most updated of each group

  • @Add the table structure and some record to http://sqlfiddle.com/ and pass the url I help you make SQL

  • http://sqlfiddle.com/#! 9/ee236 nesse não deu para colocar todo o banco de dados não sei se as informação foram combinando mas e isso ai tenho que pegar o campo data_sumula mais atual

Browser other questions tagged

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