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 9086879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:35:29+00:00 2026-06-16T21:35:29+00:00

I use Yii framework to develop a website, I want to know how to

  • 0

I use Yii framework to develop a website, I want to know how to get the last 5 images orderd by create_time but not within the same album by using Yii active record and by plain SQL.

here’s my albums table:

CREATE TABLE IF NOT EXISTS `tbl_album` (
  `album_id` int(11) NOT NULL AUTO_INCREMENT,
  `album_name` varchar(45) DEFAULT NULL,
  `album_folder_name` int(11) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `create_user_id` int(11) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `update_user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`album_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=55 ;

and here’s my images table:

CREATE TABLE IF NOT EXISTS `tbl_image` (
  `image_id` int(11) NOT NULL AUTO_INCREMENT,
  `image_name` varchar(100) DEFAULT NULL,
  `image_description` varchar(100) DEFAULT NULL,
  `image_album_id` int(11) NOT NULL,
  `create_time` datetime DEFAULT NULL,
  `create_user_id` int(11) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `update_user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`image_id`),
  KEY `fk_image_album` (`image_album_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

please note that I use “create_time” for sorting images.

Again, I need a query to get the last 5 images orderd by create_time but not within the same album by Yii active record and plain SQL.

for example the last 7 images are:

a.jpg in the album 7
b.jpg in the album 5
c.jpg in the album 7
d.jpg in the album 6
e.jpg in the album 3
f.jpg in the album 4
g.jpg in the album 2
h.jpg in the album 1

I need the query result to be like the following:

a.jpg in album 7
b.jpg in album 5
d.jpg in album 6
e.jpg in album 3
f.jpg in album 4

not like the following:

a.jpg in album 7
b.jpg in album 5
c.jpg in album 7
d.jpg in album 6
e.jpg in album 3

Thanks in advance.

  • 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-16T21:35:30+00:00Added an answer on June 16, 2026 at 9:35 pm

    Try this:

    SELECT
      a.album_name,
      i1.image_name
    FROM tbl_album a
    INNER JOIN tbl_image i1 ON i1.image_album_id = a.album_id
    INNER JOIN
    (
       SELECT image_album_id, MIN(image_id) image_id
       FROM tbl_image
       GROUP BY image_album_id
    ) i2  ON i2.image_album_id = i1.image_album_id 
         AND i1.image_id = i2.image_id
    ORDER BY a.album_name DESC
    LIMIT 5;
    

    The JOIN with:

       SELECT image_album_id, MIN(image_id) image_id
       FROM tbl_image
       GROUP BY image_album_id
    

    will insure that for each album, the first image will be returned. Therefore you will got only one image for each album, then LIMIT 5 will limit the result set to be only 5 albums.

    SQL Fiddle Demo

    Note that: The ORDER BY clause will determine which five albums will be returned by the LIMIT 5 clause. So don’t expect that the albums are returned in the way they are stored as the expected result in your question, because records in the table has no specific order. They are stored as a set, and you have to specify an ORDER BY clause in your query to get them in a specific order.


    Update: IF you are looking for the last created image for each album, use the MAX(creaet_time) instead like so:

    SELECT
      a.album_name,
      i1.image_name,
      date_format(i1.create_time, '%Y-%m-%d') create_time
    FROM tbl_album a
    INNER JOIN tbl_image i1 ON i1.image_album_id = a.album_id
    INNER JOIN
    (
       SELECT image_album_id, MAX(create_time) LatestCreateTime
       FROM tbl_image
       GROUP BY image_album_id
    ) i2  ON i2.image_album_id = i1.image_album_id 
         AND i1.create_time = i2.LatestCreateTime
    ORDER BY i1.create_time DESC
    LIMIT 5;
    

    Updated SQL Fiddle Demo


    Update 2: For the duplicate values, use the DISTINCT keyword, or you can use the MAX(image_id) instead, since the image_id is autoincremental, like so:

    SELECT
      a.album_name,
      i1.image_name,
      date_format(i1.create_time, '%Y-%m-%d') create_time
    FROM tbl_album a
    INNER JOIN tbl_image i1 ON i1.image_album_id = a.album_id
    INNER JOIN
    (
       SELECT image_album_id, MAX(image_id) LatestId
       FROM tbl_image
       GROUP BY image_album_id
    ) i2  ON i2.image_album_id = i1.image_album_id 
         AND i1.image_id = i2.LatestId
    ORDER BY i1.create_time DESC
    LIMIT 5;
    

    Updated SQL Fiddle Demo

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

Sidebar

Related Questions

I'm building a web application and eventually would want to use Yii framework. But
I use subversion to manage my yii website (php framework) and have separate backend
I am now using the framework Yii to develop a website and I need
I'm use Yii PHP framework but i cant write this code, what's the true
I have a webservice written in Yii (php framework). I use C# and Visual
I use a php framework (Yii) for my code which includes jquery.js in each
I'm creating a hybrid PHP/Node.js application. For PHP, I'm using the Yii framework. Within
With Yii php framework, I use inheritance. In my AbstractModel, I have this method:
I have recently read about csrf tokens. I am using YII framework to develop
I have a large project and want to move to the Yii Framework for

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.