Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda