PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] use group_by in codeigniter to get latest data

 February 19, 2022     codeigniter, group-by, join, php     No comments   

Issue

I am new to CodeIgniter. I have seen similar threads related my question, however I am unable find an exact solution. I have a table 'request' which contains two columns, one with 'userId' the other one with 'requestTime' as shown below (for example).

key userId requestTime
6 abc 55555
5 abc 44444
4 xyz 33333
3 abc 22222
2 xyz 11111
1 lmn 00000

Here I would like to get a list of 'userId' with latest time (maximum of 'requestTime') as below.

userId requestTime
abc 55555
xyz 33333
lmn 00000

I am using the following code in CodeIgniter.

 $this->db->order_by('request.requestTime', 'desc');
 $this->db->group_by('request.userId');
 return $this->db->get('request')->result_array();

The above CI code does not provide results as I expected. How can get the expected result.


Solution

$this->db->select_max('request.requestTime','aliasName');
$this->db->group_by('request.userId');
return $this->db->get('request')->result_array();

I have updated the answer.. hope it will help others as well.



Answered By - Maroof
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,204,559

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing