简体   繁体   English

另一个独特的MySQL限制

[英]Another distinct and limit mysql

The following does what it supposed to it returns 200+ distinct records w/o the "limit 2" 以下内容应做的是返回200多个不带“限制2”的不同记录

What I want is to return 2 distinct records, but it stops after the 1st 2 records, meaning I only get 2 records 我想要的是返回2个不同的记录,但是它在第一个2条记录之后停止,这意味着我仅获得2条记录

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. 我假设您想删除一些重复项,并获取重复次数最多的前2行数据。

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

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