[英]Mysql Query to pull latest 5 searches from table
Below is the table structure:下面是表结构:
I want to pull last 5 unique domain.我想提取最后 5 个唯一域。
I have tried following query, but it shows incorrect results - it wipes google.com from results since it also exist against prior ids: SELECT DISTINCT domain FROM searches ORDER BY id DESC 5
我尝试了以下查询,但它显示了不正确的结果 - 它从结果中擦除了 google.com,因为它也存在于先前的 id 中:
SELECT DISTINCT domain FROM searches ORDER BY id DESC 5
Can anyone suggest a fix to the query.任何人都可以建议修复查询。
SELECT DISTINCT domain
FROM your_table_name
ORDER BY domain_id DESC
LIMIT 5;
This will get the required output!!!这将获得所需的输出!!!
I think you want GROUP BY
:我想你想要
GROUP BY
:
select domain, max(search_at)
from t
group by domain
order by max(search_at) desc;
This presumes that by "latest" you mean based on the search_at
column.这假定“最新”是基于
search_at
列的意思。 You can do the same thing with the id
, if that is how you define "latest".你可以用
id
做同样的事情,如果这是你定义“最新”的方式。
Also, you do not need to include max(search_at)
in the select
.此外,您不需要在
select
包含max(search_at)
。 I put it in because you might be interested in that.我把它放进去是因为你可能对此感兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.