Есть две таблици:
CREATE TABLE IF NOT EXISTS posts
(
id int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(128) NOT NULL,
title VARCHAR(128) NOT NULL UNIQUE,
`text` VARCHAR(1000) NOT NULL,
slug VARCHAR(128) NOT NULL UNIQUE,
created_at DATE NOT NULL
);
CREATE TABLE IF NOT EXISTS comments
(
id int (10) PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(128) NOT NULL,
`text` VARCHAR(1000) NOT NULL,
postId INT,
created_at DATE NOT NULL,
CONSTRAINT post_id_fk
FOREIGN KEY (postId) REFERENCES posts (id)
);
Есть запрос:
SELECT t1.id as postId, t1.username as postUsername, t1.title as postTitle, t1.`text` as postText, t1.slug as postSlug, t1.created_at as postCreatedAt, t2.username as commentUsername, t2.`text` as commentText, t2.postId, t2.created_at as commentCreatedAt FROM posts as t1 LEFT JOIN comments as t2 ON t1.id = t2.postId WHERE t1.slug = 'Come_with_me_if_you_wanna_live';
Результат:
Нужно получить пост и все его коменты, пост приходит несколько раз. Как избавиться от повторений?