yodann
yodann

Reputation: 355

MySQL: Limit on certain field

Say I have a table with field like this:

ID       Name         Parent_ID
1       Maxim         1
2       Bruce         1
3       Jonas         3
4       Steve         4
5       Chloe         4
6       Paul          4
7       Frank         7
8       Paula         8
9       Martin        9
10      Hank          9

And I want to get a query with only top 3 different parent Ids, which would have Parent_ID of 1, 3, and 4 like below:

ID     Name         Parent_ID
1       Maxim         1
2       Bruce         1
3       Jonas         3
4       Steve         4
5       Chloe         4
6       Paul          4

How can I get this using MySQL. Can I use the LIMIT function. Can anyone help me?

Thanks.

Upvotes: 0

Views: 273

Answers (2)

wiretext
wiretext

Reputation: 3342

i hope this will help you

select t1.* from table1 as t1
inner join
(select distinct Parent_ID from table1 order by Parent_ID limit 3) as t2
on t1.Parent_ID = t2.Parent_ID

Upvotes: 2

Sabyasachi Mishra
Sabyasachi Mishra

Reputation: 1749

SELECT * FROM Tablename
-> WHERE parent_id IN ( 1,3,4 );

Upvotes: -1

Related Questions