简体   繁体   English

mysql唯一最新条目

[英]mysql unique newest entries

I have a table of urls. 我有一张网址表。 There is an extra column with the parsed domain name (foreign key to other table). 有一个额外的列,其中包含解析的域名(其他表的外键)。 What i want are the freshest urls, but only one url of each domain name. 我想要的是最新鲜的网址,但每个域名只有一个网址。 I have following schema: 我有以下架构:

id|url|id_domain

I tried severeal versions of my query but none of them seemed to work performant, such as: 我尝试了严格的查询版本,但似乎没有一个能正常工作,例如:

SELECT * from urls GROUP BY id_domain ORDER BY id DESC

Explain tells me that it uses a temporary table. 说明告诉我它使用一个临时表。

There are indices on id, id_domain, (id, id_domain) and (id_domain, id) 在id,id_domain,(id,id_domain)和(id_domain,id)上有索引

Does anybody know a more efficient way to query such data? 有谁知道查询此类数据的更有效方法?

Edit Here is some sample data with expected output: Sample data: 编辑这是一些预期输出的样本数据:样本数据:

id | url | id_domain
1 | www.example1.com/link1/index.html | 1
2 | www.example1.com/link2/index.html | 1
3 | www.example1.com/link3/index.html | 1
4 | www.example2.com/link1/index.html | 2  

i would expect the newest urls (id is auto increment, therefore higher id = newer. 我希望最新的网址(ID是自动递增,因此,较高的ID =较新。
In this case i would expect to have urls with id 3 and id 4 returned 在这种情况下,我希望返回ID为3和ID为4的网址

Thank you very much 非常感谢你

您可以在查询中使用DISTINCT。

SELECT DISTINCT id,url,id_domain from urls GROUP BY id_domain ORDER BY id DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM