简体   繁体   中英

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

在此输入图像描述

i want to select first five rows sorted by date DESC which have unique combination of both columns - tablica and 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.

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

Let me know if this works for you.

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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