我正在创建web服务来返回聊天和通知列表。用户可以将每页显示的页码和项目数作为输入发送,但可以返回两种类型的对象(从最新到最新),并且必须显示在同一列表中。
我有两张桌子chat和notification
CREATE TABLE chat
(
idchat serial NOT NULL,
idinterest integer NOT NULL,
idowner integer NOT NULL,
iduser integer NOT NULL,
creationdate,
editdate,
CONSTRAINT pk_chat PRIMARY KEY (idchat)
)
CREATE TABLE notification
(
idnotification serial NOT NULL,
message character varying(255) NOT NULL,
creationdate date NOT NULL,
datefinvalidite date NOT NULL,
idcompte integer NOT NULL,
idtypenotification integer NOT NULL,
sender integer NOT NULL DEFAULT 0,
CONSTRAINT pk_notification PRIMARY KEY (idnotification)
)我想要创建一个视图,对所有的聊天和通知进行分组,由id (idchat或idnotification)、日期(creationdate)和布尔ischat组成。
但我不知道这是否是正确的解决办法。
如果我必须返回20行有序消息(通知和聊天),问题可以这样做:
发布于 2013-05-14 16:10:13
select idchat id, creationdate, true ischat
from chat
union all
select idnotification id, creationdate, false ischat
from notification
order by creationdate desc limit 20这个版本可能会更快:
select *
from
(
(
select idchat id, creationdate, true ischat
from chat
order by creationdate desc
limit 20
)
union all
(
select idnotification id, creationdate, false ischat
from notification
order by creationdate desc
limit 20
)
) s
order by creationdate desc limit 20https://stackoverflow.com/questions/16547200
复制相似问题