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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:48:44+00:00 2026-06-02T06:48:44+00:00

I have a table containing calendar items; in my web application, I have two

  • 0

I have a table containing calendar items; in my web application, I have two views:

  1. View 1: primary view that shows the next 10 items, starting from now
  2. View 2: view that shows the previous/next 10 items based on the timestamp of the first/last item in view 1. This is the troublemaker.

On the bottom of the page, previous/next links are shown that lead to view 2.

The problem:

How do I retrieve the previous set of 10 items without knowing what date they are?

At first, this seemed quite simple to me, but apparently, it is not.

Database table:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255)     | YES  | MUL | NULL    |                |
| start_time  | datetime         | YES  |     | NULL    |                |
| end_time    | datetime         | YES  |     | NULL    |                |
| created     | datetime         | NO   |     | NULL    |                |
| updated     | datetime         | YES  |     | NULL    |                |
| deleted     | tinyint(1)       | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+

SQL query for showing next 10 items starting from now (no problems here):

SELECT ci.*
FROM `calendar_item` AS `ci` 
WHERE ci.end_time >= NOW()
GROUP BY `ci`.`id` 
ORDER BY `ci`.`end_time` ASC
LIMIT 10

SQL query for showing previous 10 items, based on timestamp of the 1st item in the primary view:

SELECT ci.*
FROM `calendar_item` AS `ci` 
WHERE (ci.id IN (
    SELECT id FROM calendar_item 
    WHERE (end_time < FROM_UNIXTIME(1334667600))
    ORDER BY end_time DESC
))
GROUP BY `ci`.`id` 
ORDER BY `ci`.`end_time` ASC
LIMIT 10

The timestamp is passed via the URL; in view 1 the subquery is not shown at all. The problem lies in the fact that items should be sorted ascending; this would result in the earliest items in the database being returned, instead of the nearest to timestamp. To counter this problem, I created a subquery that sorts descendingly. This subquery works fine when I run it as a normal query, but when contained by the above query, it simply displays the same results as would the following:

SELECT ci.*
FROM `calendar_item` AS `ci` 
WHERE ci.end_time <= 1334667600
GROUP BY `ci`.`id` 
ORDER BY `ci`.`end_time` ASC
LIMIT 10

I am most likely overlooking something, so I could use your help. 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-02T06:48:51+00:00Added an answer on June 2, 2026 at 6:48 am

    This is a simple one, LIMIT the subquery

    SELECT ci.*
    FROM `calendar_item` AS `ci` 
    WHERE (ci.id IN (
        SELECT id FROM calendar_item 
        WHERE (end_time < FROM_UNIXTIME(1334667600))
        ORDER BY end_time DESC
        LIMIT 10
    ))
    GROUP BY `ci`.`id` 
    ORDER BY `ci`.`end_time` ASC
    LIMIT 10
    

    Without the limit in the subquery you are selecting ALL rows with a timestamp < FROM_UNIXTIMESTAMP. You are then reordering ASC and selecting the first 10, i.e. the earliest 10.

    If you limit the subquery you get the 10 highest which satisfy your FROM_UNIXTIME, and the outer can then select them.

    An alternative (and my preferred) would be the following, where the subquery gets the data, and the outer query simply reorders it before spitting it back out.

    SELECT i.*
    FROM (
        SELECT ci.*
        FROM calendar_item AS ci
        WHERE ci.end_time < FROM_UNIXTIME(1334667600)
        ORDER BY ci.end_time DESC
        LIMIT 10
    ) AS i
    ORDER BY i.`end_time` ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing view/click records. The time is stored in a unix
I have a table containing transaction information on items. The possible transactions are purchase
I have a Table containing some information that I need. All these rows also
I'm writing a WPF desktop application and have a view that needs to look
Lets say that I have a table containing users . Each row in that
Ok, so I have a table containing information about jobs. The goal is that
I have a table containing some categories Categories Table: -- category_id <-- primary key
I have a table containing some Timespans (as two TIME columns) Eg: TimeBegin TimeEnd
I have a table containing pipes that are installed at a location. Installed dates
I have a table containing events which happen in my application like people logging

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.