MYSQL - Is there an alternative to LIMIT in the Queries?

Asked

Viewed 18 times

0

I’m using MYSQL. I have the following table:

create table visita (
   id int,
   titulo_socio varchar(10),
   data_visita date,
   cidade_visitada varchar(50),
   status_visitacao varchar(50)
);

A data example for this table:

id   titulo_socio   data_visita   cidade_visitada   status_visitacao

10   11111          2005-02-12    Goiania/GO        Sim
11   11111          2010-05-19    Marilia/SP        Sim
12   11111          2015-01-23    Brasilia/DF       Nao
13   22222          2014-01-02    Brasilia/DF       Sim
14   22222          2012-10-21    Goiania/GO        Nao
15   33333          2010-08-09    Marilia/SP        Nao

I need an SQL that returns only the most current date line of each partner, thus:

id   titulo_socio   data_visita   cidade_visitada   status_visitacao

12   11111          2015-01-23    Brasilia/DF       Nao
13   22222          2014-01-02    Brasilia/DF       Sim
15   33333          2010-08-09    Marilia/SP        Nao

I’m doing it using subquery, but the MYSQL did not accept LIMIT:

SELECT titulo_socio, data_visita, cidade_visitada, status_visitacao from visita WHERE (titulo_socio, data_visita) IN (SELECT titulo_socio, max(data_visita) FROM visita GROUP BY titulo_socio LIMIT 500);

Without the LIMIT works, but I need to limit because the table has more than 30 million tuples and it is impossible to consult.

Is there any other alternative way to get this result without using LIMIT in the subquery?

Thanks

  • You can do a grouping by pk and sort by date in descending mode, it serves?

  • Thank you for the reply. Could you give me an example? Thank you

  • I solved the same problem in this POST: https://answall.com/questions/370267/recuperar-o-maior-valor-em-uma-coluna-de-acordo-a-repeti%C3%A7%C3%A3o-da-segunda/370275

No answers

Browser other questions tagged

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