SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE "comments" ( "id" serial, "author_id" bigint NOT NULL, "post_id" bigint NOT NULL, "text" text NOT NULL, "parent_id" bigint, "created_at" timestamp(0), "updated_at" timestamp(0), "deleted_at" timestamp(0), CONSTRAINT "comments_pkey" PRIMARY KEY ("id") ) WITH (oids = false); INSERT INTO "comments" ("id", "author_id", "post_id", "text", "parent_id", "created_at", "updated_at", "deleted_at") VALUES (44, 1, 107, 'вввв11в', NULL, '2022-08-31 12:59:44', '2022-08-31 12:59:44', NULL), (45, 1, 107, 'ввввв', NULL, '2022-08-31 12:59:44', '2022-08-31 12:59:44', NULL), (46, 1, 108, 'вв3вв', NULL, '2022-08-31 12:59:47', '2022-08-31 12:59:47', NULL), (47, 1, 107, 'ыыы', NULL, '2022-08-31 12:59:57', '2022-08-31 12:59:57', NULL), (48, 1, 107, 'as11d', NULL, '2022-08-31 13:05:00', '2022-08-31 13:05:00', NULL), (49, 1, 106, 'asd', NULL, '2022-08-31 13:05:01', '2022-08-31 13:05:01', NULL), (50, 2, 106, 'dwwd', NULL, '2022-08-31 13:05:09', '2022-08-31 13:05:09', NULL); WITH posts(post_id) AS (VALUES (106), (107), (108)) SELECT * FROM posts JOIN LATERAL (SELECT * FROM comments WHERE comments.post_id=posts.post_id ORDER BY created_at DESC LIMIT 3) AS comments ON True;
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear