【发布时间】:2020-11-21 04:33:39
【问题描述】:
我有这张桌子:
消息
/*Table structure for table `messages` */
CREATE TABLE `messages` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`fromperson` varchar(255) NOT NULL,
`sent` datetime(6) NOT NULL,
`msgread` int(2) DEFAULT 0,
`content` text DEFAULT NULL,
`toperson` varchar(255) NOT NULL,
`route` int(2) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=75508 DEFAULT CHARSET=utf8mb4;
/*Data for the table `messages` */
insert into `messages`(`id`,`fromperson`,`sent`,`msgread`,`content`,`toperson`,`route`) values
(75477,'jaritje','2020-07-31 11:47:59.000000',1,'helemaal niks :)','anaisje',0),
(75478,'jaritje','2020-07-31 11:48:25.000000',1,'wdj','anaisje',1),
(75479,'jaritje','2020-05-25 12:57:27.000000',1,'cv','anaisje',0),
(75501,'jaritje','2020-05-25 13:38:31.000000',1,'gmj*','anaisje',1),
(75502,'jaritje','2020-05-25 13:38:48.000000',1,'gm','anaisje',1),
(75503,'jaritje','2020-05-26 16:53:27.000000',1,'hgh','anaisje',0),
(75504,'jaritje','2020-05-26 17:05:27.000000',1,'hey\r\n','anaisje',1),
(75505,'jaritje','2020-05-26 18:14:03.000000',1,'hallo','anaisje',0),
(75507,'jaritje','2020-07-22 12:57:27.000000',1,'TEST ','saartje',1);
现在我想选择每个人的每条最新消息。 所以最近的消息是“anaisje”和“saartje”。
所以我想要 id 为 75478 和 75507 的行。
我在网上看到
SELECT * FROM (SELECT * FROM messages ORDER BY sent DESC) AS person WHERE fromperson = ?
应该可以,但它不适合我...
谁能帮我解决这个问题? 提前致谢,
贾里
【问题讨论】:
-
FIRST_VALUE(message) OVER (PARTITION BY person ORDER BY timestamp DESC) -
那是什么意思?
-
这是一个可以解决你的任务的表达式。如果您想要更详细的答案,请将照片替换为 CREATE TABLE 脚本、INSERT INTO 脚本与一些示例数据,显示此数据的所需输出,并要求版主重新打开问题。
-
我如何向版主询问重新打开的问题?我添加了创建表并插入到脚本中。提前致谢
-
我没有看到 此数据的所需输出。如果某人不知道需要什么结果,他如何提供帮助?