简体   繁体   English

MySQL根据两列选择前五个唯一行

[英]MySQL select first five unique rows depending on two columns

I have a mysql table which have two columns for selecting propouses - tablica and contentid as it shows here 我有一个mysql表有两列用于选择propouses - tablica和contentid, 如这里所示

在此输入图像描述

i want to select first five rows sorted by date DESC which have unique combination of both columns - tablica and contentid 我想选择按日期DESC排序的前五行,它们具有两列的唯一组合 - tablica和contentid

how that can happend? 怎么会发生? I've tried already with combinations of distinct, group_concat and concat but everything i've tried skip some of the rows. 我已经尝试过使用distinct,group_concat和concat的组合,但我尝试过的所有内容都跳过了一些行。

please help. 请帮忙。

Hello_ mate 你好伙伴

If you don't need other stuff than those two fields you can use the query I did post in the comments below your question. 如果您不需要除这两个字段之外的其他内容,您可以使用我在您的问题下面的评论中发布的查询。

Otherwise if you need all the fields you can use this query: 否则,如果您需要所有字段,则可以使用此查询:

SELECT `id`, `tablica`, `contentid`, `ip`, `userid`, MAX(date) as `date` 
FROM test2 
GROUP BY tablica, contentid 
ORDER BY date DESC 
LIMIT 5;

using the MAX function we select the latest matching date for this tablica and contentid , if you want to get oldest date use MIN function 使用MAX函数我们选择此tablicacontentid的最新匹配日期,如果你想获得最早的日期使用MIN函数

Let me know if this works for you. 如果这对您有用,请告诉我。

Good Luck! 祝好运!

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

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