如何从回复和帖子中获取最后5条帖子(按日期(created_at))。
示例胜于解释:
StateEndTime
预期结果是:
-- topics --
id - title - content - created_at
----------------------------------------
1 - title - content - 2019-02-05 19:14:41
2 - title2 - content2 - 2019-02-05 19:14:42
3 - title3 - content3 - 2019-02-05 19:14:43
4 - title4 - content4 - 2019-02-05 19:14:44
5 - title5 - content5 - 2019-02-05 19:14:45
-- replies --
id - content - created_at
--------------------------
1 - content - 2019-02-05 19:14:41
2 - content2 - 2019-02-05 19:14:42
3 - content3 - 2019-02-05 19:14:43
4 - content4 - 2019-02-05 19:14:44
5 - content5 - 2019-02-05 19:14:45
我正在使用MySQL
答案 0 :(得分:1)
这应该回答您的问题
模式(MySQL v5.7)
CREATE TABLE topics (
`id` INTEGER,
`title` VARCHAR(6),
`content` VARCHAR(8),
`created_at` datetime
);
INSERT INTO topics
(`id`, `title`, `content`, `created_at`)
VALUES
('1', 'title', 'content', '2019-02-05 19:14:41'),
('2', 'title2', 'content2', '2019-02-05 19:14:42'),
('3', 'title3', 'content3', '2019-02-05 19:14:43'),
('4', 'title4', 'content4', '2019-02-05 19:14:44'),
('5', 'title5', 'content5', '2019-02-05 19:14:45');
CREATE TABLE replies (
`id` INTEGER,
`content` VARCHAR(8),
`created_at` datetime
);
INSERT INTO replies
(`id`, `content`, `created_at`)
VALUES
('1', 'content', '2019-02-05 19:14:41'),
('2', 'content2', '2019-02-05 19:14:42'),
('3', 'content3', '2019-02-05 19:14:43'),
('4', 'content4', '2019-02-05 19:14:44'),
('5', 'content5', '2019-02-05 19:14:45');
查询#1
select * from
(
select * from topics
union all
select id, null, content, created_at from replies
) t
ORDER BY created_at desc
LIMIT 5;
结果
| id | title | content | created_at |
| --- | ------ | -------- | ------------------- |
| 5 | | content5 | 2019-02-05 19:14:45 |
| 5 | title5 | content5 | 2019-02-05 19:14:45 |
| 4 | | content4 | 2019-02-05 19:14:44 |
| 4 | title4 | content4 | 2019-02-05 19:14:44 |
| 3 | | content3 | 2019-02-05 19:14:43 |
答案 1 :(得分:1)
您可以合并两个获取所有信息的查询,按日期排序,然后限制为5个结果
模式(MySQL v5.7)
CREATE TABLE topics (
`id` INTEGER,
`title` VARCHAR(6),
`content` VARCHAR(8),
`created_at` DATETIME
);
INSERT INTO topics
(`id`, `title`, `content`, `created_at`)
VALUES
(1, 'title', 'content', '2019-02-05 19:14:41'),
(2, 'title2', 'content2', '2019-02-05 19:14:42'),
(3, 'title3', 'content3', '2019-02-05 19:14:43'),
(4, 'title4', 'content4', '2019-02-05 19:14:44'),
(5, 'title5', 'content5', '2019-02-05 19:14:45');
CREATE TABLE replies (
`id` INTEGER,
`content` VARCHAR(8),
`created_at` DATETIME
);
INSERT INTO replies
(`id`, `content`, `created_at`)
VALUES
(1, 'content', '2019-02-05 19:14:41'),
(2, 'content2', '2019-02-05 19:14:42'),
(3, 'content3', '2019-02-05 19:14:43'),
(4, 'content4', '2019-02-05 19:14:44'),
(5, 'content5', '2019-02-05 19:14:45');
查询#1
SELECT id, CONCAT(title, ' - ', content) AS "title - content", created_at
FROM topics
UNION ALL
SELECT id, content, created_at
FROM replies
ORDER BY created_at DESC LIMIT 5;
输出
| id | title - content | created_at |
| --- | ----------------- | ------------------- |
| 5 | title5 - content5 | 2019-02-05 19:14:45 |
| 5 | content5 | 2019-02-05 19:14:45 |
| 4 | content4 | 2019-02-05 19:14:44 |
| 4 | title4 - content4 | 2019-02-05 19:14:44 |
| 3 | content3 | 2019-02-05 19:14:43 |
查询#2
-- If you want to sort them by date ascending, you can nest the previous query into another one, that you sort by date ascending
SELECT * FROM
(
SELECT id, CONCAT(title, ' - ', content) AS "title - content", created_at
FROM topics
UNION ALL
SELECT id, content, created_at
FROM replies
ORDER BY created_at DESC LIMIT 5
) subQuery
ORDER BY subQuery.created_at ASC;
输出
| id | title - content | created_at |
| --- | ----------------- | ------------------- |
| 3 | content3 | 2019-02-05 19:14:43 |
| 4 | content4 | 2019-02-05 19:14:44 |
| 4 | title4 - content4 | 2019-02-05 19:14:44 |
| 5 | content5 | 2019-02-05 19:14:45 |
| 5 | title5 - content5 | 2019-02-05 19:14:45 |