Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:03:06+00:00 2026-06-12T06:03:06+00:00

I am basically trying to bring the last 5 records from a table. The

  • 0

I am basically trying to bring the last 5 records from a table.
The only catch is that I don’t want some records with the same x_id to be brought since I would be repeating the x_id, so I pass a ‘group’ condition to achieve this.
However, when adding this new group condition, something odd happens and the result for the query is randomly omitting some rows of the table. So, randomly, some records are brought, and some are not. So some record which should have been brought is not.

For instance:
LAST 10 records in my table are: 1-2-3-4-5-6-7-8-9-0
After doing my query, I should have as a result: 0-9-8-7-6
However, at random, I get: 9-8-6-5-3 OR 0-8-7-6-4

Basically it skips some records the shit =//

$condicionesComentarios = array('conditions' => array('Comentario.aceptado' => 1), 'limit' => 5, 'order' => 'Comentario.id DESC', 'group' => array('Comentario.empresa_id'));

$ultimosComentarios = $this->Empresa->Comentario->find('all', $condicionesComentarios);

UPDATE FOR CLARIFICATION

Empresa hasMany Comentario

Small portion of Comentarios table:

**id**     **empresa_id**
1           7
2           9 
3           3
4           1
5           1
6           4
7           8
8           5

EXPECTED RESULT: last 5 records from the table, without repeated empresa_id

query result-> (id = 8, 7, 6, 5, 3) *Note that row with id: 4 should be skipped, to avoid empresa_id repetition*

WHAT I GET: Some of the records are randomly skipped when they should not.

query result-> (id = 7, 6, 5, 2, 1) Note that here row with id: 8 should not be skipped, but it’s nonetheless.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:03:07+00:00Added an answer on June 12, 2026 at 6:03 am

    Joni you were almost right, put me on track. Your code perse was not working, though a small modification made it work properly:

    $condicionesComentarios = array(
                              'conditions' => array('Comentario.aceptado' => 1), 
                              'limit' => 5, 'order' => 'max(Comentario.id) DESC', 
                              'group' => array('Comentario.empresa_id'), 
                              'fields' => array('max(Comentario.id)', 'Comentario.empresa_id'));
    

    As you can see I had to modify the ‘order’ and add an extra condition to ‘fields’.
    Again what it does: brings the last 5 rows from Comentarios table, but without repeating the empresa_id field from within those last rows.

    Thanks for the help 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to copy one table over another one atomically. Basically I want
I'm trying to basically trying to separate a specific amount of text from the
I'm basically trying to extract a Mac application from a zip file using QuaZip.
I am trying to port some code written in MATLAB to C, so that
Ok, so i'm basically trying to load the contents of a .txt file that
Here's what I'm trying to do: I want to put away some methods into
I'm basically trying to iterate through a dict and print out the key /
I'm basically trying to create a url friendly query string with user input. The
I'm basically trying to make my js and css files dynamic. I've tried a
What im basically trying to do is to confirm the identity of the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.