SELECT id, server_id, start_time, end_time
FROM errors
WHERE server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
ORDER BY id DESC
LIMIT 9
这是我尝试运行以提供
server_id
= 3, 12, 24, 25, 26, 27, 28, 29, 30
的结果的查询。相反,我收到的是server_id
= 25, 25, 12, 25, 27, 27, 28, 28, 27
。请注意重复的server_ids。查询给我唯一的id
但重复的server_id
。有什么方法可以使我得到每个
id
的最后一个server_id
的结果?我尝试做
ORDER BY server_id
,但这给了我同样的问题。我尝试运行
DISTINCT
,但这也行不通。 最佳答案
您将必须使用一些汇总功能。
就像是
select
server_id,
max(id),
avg(start_time),--for example
avg(end_time)--for example
from errors
where server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
group by server_id
order by id desc
如果您需要与server_id对应的最大ID的start_time和end_time,则可以
select e.id, e.server_id, e.start_time, e.end_time
from errors e
join (select server_id, max(id) maxid
from errors
group by server_id) t
on t.maxid = e.id and e.server_id = t.server_id
where e.server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
order by e.id DESC
关于mysql - MySQL在哪里和不同和限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24679574/