【发布时间】:2021-10-07 22:27:34
【问题描述】:
我有这 2 个表,我正在尝试编写一个查询来帮助我选择所有给出此结果的行
用户
| id | name |
|---|---|
| 1 | test |
| 2 | test2 |
日志
| id | userId | message | date |
|---|---|---|---|
| 1 | 1 | this is a test | 2020-10-07 12:57:14 |
| 2 | 1 | another reason | 2020-10-07 13:57:14 |
| 3 | 1 | another reason 2 | 2020-10-07 14:57:14 |
| 4 | 2 | another reason 3 | 2020-10-04 12:57:14 |
| 5 | 2 | another reason 4 | 2020-10-05 12:57:14 |
| 6 | 2 | another reason 4 | 2020-10-06 12:57:14 |
输出表 我需要像在这种情况下(1,2)一样传递许多用户ID,并在表格下方仅返回每个用户ID每行的MAX(日期)
| id | userId | message | date |
|---|---|---|---|
| 3 | 1 | another reason 2 | 2020-10-07 14:57:14 |
| 6 | 2 | another reason 4 | 2020-10-06 12:57:14 |
这可以通过一个查询实现吗?这是我有但不工作的东西
SELECT
id ,
userId ,
message,
date
FROM logs
WHERE userId IN (1,2)
ORDER BY date DESC;
【问题讨论】: