简体   繁体   中英

Another distinct and limit mysql

The following does what it supposed to it returns 200+ distinct records w/o the "limit 2"

What I want is to return 2 distinct records, but it stops after the 1st 2 records, meaning I only get 2 records

select distinct LEFT(`name`, LOCATE("(", `name`)-1), user_id, id
from ppbv79_listings
where  user_id = 3798 and category_id = 30 
group by LEFT(`name`, LOCATE("(", `name`)-1)
limit  2

Name                              user_id   id
Germany 1213 Used Carl Sonnenschein 3798    2160555 
Germany 1213 Used Carl Sonnenschein 3798    2160556

Try this:

select A.`trimmedName`,  A.user_id, A.id 
from
(select LEFT(`name`, LOCATE("(", `name`)-1) 
             `trimmedName`, user_id, id,count(category_id) `count`
 from ppbv79_listings
 where  user_id = 3798 and category_id = 30 
 group by LEFT(`name`, LOCATE("(", `name`)-1), user_id, id 
 order by `count` desc) A
 limit  2;

I assume there are some repetitions you would want to removed, and fetch the just the top 2 rows of data that repeats most.

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