Return only the newest record of each author

Asked

Viewed 992 times

4

I have a table with the following fields:

id, texto, autor_id, data

I need the records to be grouped by autor_id (because it has several texts with the same author) and return me in ascending order the newest only. I’m using Lavarel and my Eloquent code is as follows::

Discursos::take(10)->distinct('membro_id')->orderBy('data_discurso', 'asc')->get();

Place groupBy in place of distinct is returning me only the oldest of the records. How to do otherwise?

What I am doing is returning a text list in which the author has not registered for a long time, I want you to return to me to know which author has not registered anything for a long time.

  • To explain to you... I have a table with the record of speeches, what I need is a control in which will list me the discusantes that the longest time has no speech recorded, so I can warn you that it has been a long time that he does not register a speech. Thank you

3 answers

1


The query you need, in pure SQL, is like this:

SELECT membro_id, MAX(data_discurso)
FROM tabela
GROUP BY membro_id
ORDER BY MAX(data_discurso) DESC, membro_id

If you need any more fields in the SELECT (for example, the text itself), you will need to use a subquery.

Using Eloquent the above query would look like this:

$resultado = DB::table('tabela')->select(DB::raw('membro_id, MAX(data_discurso)'))
                     ->groupBy('membro_id')
                     ->orderBy(DB::raw('MAX(data_discurso) DESC, membro_id'))->get();

Edited: Eloquent accepts Query Builder expressions, such as wherein, you just need to edit the sub query:

Discursos::whereIn(DB::raw('SELECT MAX(data_discurso) FROM discursos GROUP BY membro_id'))

For more information see the documentation:

http://laravel.com/docs/eloquent

http://laravel.com/docs/queries

  • Thanks, @gmsantos! I’m marking the response as a community wiki to not earn points with it, since only half is mine :)

  • I got what I needed by doing the following with Query Builder: DB::select(DB::raw('SELECT * FROM Speech WHERE data_speech IN (SELECT MAX(data_speech) FROM Speech GROUP BY membro_id) ORDER BY data_speech ASC')); I couldn’t find a solution with Eloquent, the hard that this query does not automatically bring back my relationships that I have on this table, thing that eloquent does. Do you have a way to put this query in Eloquent?

  • I don’t know the eloquent, so I can’t help much...

  • I edited the previous answer.

  • @gmsantos now the answer is more yours than mine :) if you want to post a separate answer and earn points with her, do this and let me know, I delete mine.

  • @bfavaretto no problem, can leave anyway

Show 1 more comment

1

The problem is that you are asking for data sorting to be field-based data_discurso in order ascendant (from the oldest to the youngest), when in fact it should be descending (from the newest to the oldest). The method get() returns always the first search result a group of ten records (by the use of take(10)) and in this case the first record should be the oldest. To correct, simply change the following section

->orderBy('data_discurso', 'asc')

for

->orderBy('data_discurso', 'desc')
  • Even with the order changed does not bring me the records as needed, always brings the first date of each speech. See my comment in the colleague’s reply below, I have the SQL query to do what I need. Thank you

-2

Order dates in descending order:

Discursos::take(10)->distinct('membro_id')->orderBy('data_discurso', 'desc')->get();
  • 2

    The answer becomes more complete if you explain the why of your solution. Put links to documentation also values. Check out the guide [Answer].

  • 1

    Welcome to Stackoverflow! It would be interesting if you explained how the code posted answers the question.

  • I would also like to see an explanation of how this code works. The more detailed your answer will be, the easier it will be for someone who comes in the future and wants to learn a little more about the [tag:].

  • 1

    GroupBy or distinct gives me the same data output. Thank you

Browser other questions tagged

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