user1598019
user1598019

Reputation:

Limiting SQL results

I have a SQL query that gives me the following results:

UserID | Msg

1      | Hello 
1      | What's up? 
3      | Blabla..

Is there something like an advanced LIMIT?

If simply LIMIT this result saying LIMIT 0,2 I'll get only the Msg's from user 1, but I want to get the Msgs from the first two users in the result (while not knowing what their ids are!)

Any help highly appreciated!

Upvotes: 1

Views: 72

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562230

This returns what you asked for:

SELECT t.* 
FROM (SELECT DISTINCT UserID FROM t LIMIT 2) AS u 
INNER JOIN t USING (UserID);

Upvotes: 1

u_mulder
u_mulder

Reputation: 54831

No advanced LIMIT are available. For your situation you can use something like:

SELECT * FROM `table_name` 
WHERE UserID IN (
    SELECT DISTINCT UserID FROM `table_name` ORDER BY UserID ASC LIMIT 2
)

Upvotes: 2

Related Questions