具有错误的postgres sql中的group by必须出现在GROUP BY子句中或用于聚合函数

时间:2017-06-14 13:16:48

标签: postgresql

我的查询如下:

SELECT "post".*, "schedule"."from_time" AS "scheduleFrom", "schedule"."to_time" AS "scheduleTo", "schedule"."id" AS "scheduleId" 
FROM "post" LEFT JOIN "post_schedule" ON "post"."id" = "post_schedule"."post_id" LEFT JOIN "schedule" ON "post_schedule"."schedule_id" = "schedule"."id" 
WHERE ("post"."type"='session') AND ("schedule"."from_time" >= 1497441023 or "post_schedule"."is_repeat" = TRUE) 
AND (post.id = 72 or post.id = 219 or post.id = 227 or post.id = 220 or post.id = 221)
AND (schedule.status = '1') 
Group by post.id

使用mysql可以正常工作,但是在postgres中我有一个错误:必须出现在GROUP BY子句中或者用在聚合函数中。

这是表格

CREATE TABLE public.post
(
    id integer NOT NULL DEFAULT nextval('post_id_seq'::regclass),
    name character varying(255) COLLATE pg_catalog."default" NOT NULL,
    excerpt character varying(255) COLLATE pg_catalog."default",
    content text COLLATE pg_catalog."default",
    author_id integer,
    published_at integer,
    slug character varying(255) COLLATE pg_catalog."default",
    type character varying(10) COLLATE pg_catalog."default",
    meta_title character varying(255) COLLATE pg_catalog."default",
    meta_description character varying(255) COLLATE pg_catalog."default",
    meta_keywords character varying(255) COLLATE pg_catalog."default",
    status smallint NOT NULL DEFAULT 10,
    created_at integer NOT NULL,
    updated_at integer NOT NULL,
    created_by integer NOT NULL,
    updated_by integer NOT NULL,
    CONSTRAINT post_pkey PRIMARY KEY (id),
    CONSTRAINT "fk-post-author_id" FOREIGN KEY (author_id)
        REFERENCES public."user" (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

post_schedule

CREATE TABLE public.post_schedule
(
    id integer NOT NULL DEFAULT nextval('post_schedule_id_seq'::regclass),
    post_id integer NOT NULL,
    schedule_id integer NOT NULL,
    is_repeat boolean DEFAULT false,
    CONSTRAINT post_schedule_pkey PRIMARY KEY (id),
    CONSTRAINT "fk-post_schedule-post_id" FOREIGN KEY (post_id)
        REFERENCES public.post (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE,
    CONSTRAINT "fk-post_schedule-schedule_id" FOREIGN KEY (schedule_id)
        REFERENCES public.schedule (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE
)

和表格时间表

CREATE TABLE public.schedule
(
    id integer NOT NULL DEFAULT nextval('schedule_id_seq'::regclass),
    name character varying(255) COLLATE pg_catalog."default",
    status character varying(255) COLLATE pg_catalog."default",
    "from" character varying(255) COLLATE pg_catalog."default",
    "to" character varying(255) COLLATE pg_catalog."default",
    created_at integer NOT NULL,
    updated_at integer NOT NULL,
    created_by integer NOT NULL,
    updated_by integer NOT NULL,
    from_time integer,
    to_time integer,
    CONSTRAINT schedule_pkey PRIMARY KEY (id)
)

我通过非组查询时的数据示例: here

如何获得第一行foreach post.id?

0 个答案:

没有答案