Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Get records from last hour or last 20 items if there is none in the last hour

I have a chat system and I want to show messages sent in the last hour, but I also want to show the last 20 messages no matter how long ago they were sent.

Is there a way I can do this in.a SQL query?

CREATE TABLE IF NOT EXISTS `chat` (
  `id`        INT(11) UNSIGNED                                                                            NOT NULL AUTO_INCREMENT,
  `user_id`   INT(11) UNSIGNED                                                                            NOT NULL,
  `item_id`   INT(11) UNSIGNED                                                                            NOT NULL,
  `message`   TEXT                                                                                        NOT NULL,
  `recipient` INT(11)                                                                                     NOT NULL DEFAULT '0',
  `type`      ENUM ('message', 'announcement') NOT NULL DEFAULT 'message',
  `channel`   ENUM ('general', 'private')                                              NOT NULL DEFAULT 'general',
  `posted`    DATETIME                                                                                    NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `posted` (`posted`),
  KEY `type` (`type`),
  KEY `channel` (`channel`),
  KEY `recipient` (`recipient`)
)
  ENGINE = MyISAM
  DEFAULT CHARSET = `utf8`
  AUTO_INCREMENT = 2;
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This should do the trick:

(
    select * from chat
    where timestamp > DATE_SUB(now(), interval 1 hour)
)
union
(   
    select * from chat order by posted desc limit 20
)
order by posted

Explanation:

  • It selects all messages from the last hour.
  • It also selects the last 20 messages, as a separate query (20 records is fast)
  • The union merges the two results, and removes doubles in case there's overlap between the two

So, when there is no data in the past hour, you'll still get the 20 most recent posts. If there's a lot of data in the past hour, you'll get all of those.

over 4 years ago · Santiago Trujillo Report

0

Try this:

SELECT *
FROM `chat`
ORDER BY `posted` DESC
LIMIT 0,20;

It will list the last 20 rows (by date/time). Since you want older chats when there aren't enough within the last hour, you don't need to worry about the age of the chats.

The following is untested and may have a syntax error, so you may have to play with it a bit.

SELECT *
FROM `chat`
WHERE DATE_ADD(a.`posted` interval 1 hour) <= NOW()
UNION
(
    SELECT *
    FROM `chat`
    WHERE a.posted > DATE_SUB(NOW(), interval 1 hour)
    AND (SELECT count(*) FROM `chat` WHERE a.posted <= DATE_SUB(NOW(), interval 1 hour)) < 20
    ORDER BY `posted` DESC
    LIMIT 0,20)
ORDER BY `posted` DESC

It should only add the posts older than the last hour when there are less than 20 posts in the last hour.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!