开发者

Mysql select distinct but limit the distinct values

This might be confusing but what I want to do is a basic select distinct columnx limit 3, the problem I have is it returns 3 rows, but I only want to get all values from one distinct columnx.

id|columnx  
1 |here  
2 |here  
3 |Idontwant  
4 |Apple

so I want a query that will return 1 and 2. The problem is columnx could be anything and I cant just say where columnx = 'here'

The limit 3 comes into play because it's hard coded into my C# app. The issue is i also set a hashset based on columnx, i have to have that columnx static for all records. However, with every query i put together there is the possibility that with my limit i'll return 2 values in columnx, which are distinct values however, i can only have distinct columnx value per query.

No what I want is all 开发者_如何学运维values that belong to columnx, in this example columnx ='here', when i do a distinct all I want back is the first distinct result, not 'Idontwant' or 'apple' regardless of the limit.

Here is the query and it works.

SELECT id,columnx
FROM sites where columnx= (select distinct columnx from sites limit 1) limit 3


It sounds like you want the id values for three distinct columnx values. Is this right? Try this:

SELECT id, columnx
FROM table AS t1
JOIN (
    SELECT DISTINCT(columnx) AS columnx
    LIMIT 1
) AS t2 ON (t1.columnx = t2.columnx);

I also wonder if you need some ORDER BY in there somewhere, but your question doesn't mention that, so I've omitted it--essentially meaning that you'll get three semi-random columnx values.


To be honest I'm not sure what you are asking either but it sounds like you want to return the ids for those rows where there are duplicate values in columnx (limit the result to 3).

SELECT id, columnx
FROM tablename
GROUP BY columnx
HAVING ( COUNT(columnx) > 1 )
LIMIT 3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜