要求使用SQL和/或JPQL语句检索按日期(降序)排序的给定帐户的最新记录。它应该为所有用户返回一条记录(最新记录),例如如果“用户a”从“用户b”,“用户c”和“用户d”发送/接收多条消息,它将返回每个用户的最新记录(共3条)。
我对JPQL的尝试:
SELECT DISTINCT dm FROM DirectMessage dm WHERE (dm.messageFrom.id=1 OR dm.messageTo.id=1) GROUP BY dm.id, dm.messageFrom.id, dm.messageTo.id ORDER BY dm.date DESC
具有架构的具体示例:
如果我希望返回“用户a”的最新记录,则返回的正确记录数将为3(以下这3条记录的每条插入语句):
1.INSERT INTO direct_message VALUE(2, 2, 1, 'hello back', now());
2.INSERT INTO direct_message VALUE(4, 3, 1, 'hello back', now());
3.INSERT INTO direct_message VALUE(6, 4, 1, 'hello back', now());
脚本
CREATE TABLE account(
id bigint UNSIGNED NOT NULL auto_increment,
username varchar(75) NOT NULL,
name varchar(70) NOT NULL,
date datetime NOT NULL,
CONSTRAINT pkey_account_id PRIMARY KEY(id)
);
INSERT INTO account VALUES (1,'user_a','User A', now());
INSERT INTO account VALUES (2,'user_b','User B', now());
INSERT INTO account VALUES (3,'user_c','User C', now());
INSERT INTO account VALUES (4,'user_d','User D', now());
CREATE TABLE direct_message(
id bigint UNSIGNED NOT NULL auto_increment,
message_from_id bigint UNSIGNED NOT NULL,
message_to_id bigint UNSIGNED NOT NULL,
message_text varchar(200) NOT NULL,
date datetime NOT NULL,
CONSTRAINT pkey_dm_id PRIMARY KEY(id),
CONSTRAINT fk_direct_message_to FOREIGN KEY (message_to_id) REFERENCES account(id),
CONSTRAINT fk_message_from FOREIGN KEY (message_from_id) REFERENCES account(id)
);
INSERT INTO direct_message VALUE(1, 1, 2, 'hello', now());
INSERT INTO direct_message VALUE(2, 2, 1, 'hello back', now());
INSERT INTO direct_message VALUE(3, 1, 3, 'hello', now());
INSERT INTO direct_message VALUE(4, 3, 1, 'hello back', now());
INSERT INTO direct_message VALUE(5, 1, 4, 'hello', now());
INSERT INTO direct_message VALUE(6, 4, 1, 'hello back', now());
INSERT INTO direct_message VALUE(7, 2, 4, 'hello', now());
INSERT INTO direct_message VALUE(8, 4, 2, 'hello back', now());
答案 0 :(得分:0)
我想你想要
select dm.*
from direct_message dm
where @userid in (dm.message_from_id, dm.message_to_id) and
dm.date = (select max(dm2.date)
from direct_message dm2
where (dm2.message_from_id, dm2.message_to_id) in ( (dm.message_from_id, dm.message_to_id), (dm.message_to_id, dm.message_from_id) )
);
编辑:
有不同的消息具有完全相同的日期/时间戳似乎很奇怪,但是您可以使用id来区分它们:
select dm.*
from direct_message dm
where 1 in (dm.message_from_id, dm.message_to_id) and
dm.id = (select dm2.id
from direct_message dm2
where (dm2.message_from_id, dm2.message_to_id) in ( (dm.message_from_id, dm.message_to_id), (dm.message_to_id, dm.message_from_id) )
order by dm2.date desc
limit 1
);