Mysql
 SQL >> Base de Dados >  >> RDS >> Mysql

Sistema de notificações com PHP e MySQL

Um modelo simples armazena cada notificação com flag de leitura, remetente, destinatário, tipo e referência.

CREATE TABLE notifications (
  id INT AUTO_INCREMENT PRIMARY KEY,
  recipient_id INT NOT NULL,
  sender_id INT NOT NULL,
  unread TINYINT(1) NOT NULL DEFAULT 1,
  type VARCHAR(255) NOT NULL,
  parameters TEXT,
  reference_id INT,
  created_at INT NOT NULL
);

Agrupe notificações semelhantes na consulta:

SELECT type, reference_id, COUNT(*) AS total
FROM notifications
WHERE recipient_id = ?
GROUP BY type, reference_id
ORDER BY MAX(created_at) DESC
LIMIT 20;

Use tipos para gerar mensagens dinâmicas e evitar duplicatas. A camada de aplicação deve controlar criação e leitura.