SQL Complicated Group / Join by Category

Posted by Mike Silvis on Stack Overflow See other posts from Stack Overflow or by Mike Silvis
Published on 2010-05-12T08:08:58Z Indexed on 2010/05/12 8:14 UTC
Read the original article Hit count: 282

Filed under:
|
|

I currently have a database structure with two important tables.

1) Food Types (Fruit, Vegetables, Meat)

2) Specific Foods (Apple, Oranges, Carrots, Lettuce, Steak, Pork)

I am currently trying to build a SQL statement such that I can have the following.

Fruit < Apple, Orange

Vegetables < Carrots, Lettuce

Meat < Steak, Port

I have tried using a statement like the following

Select * From Food_Type join (Select * From Foods) as Foods on Food_Type.Type_ID = Foods.Type_ID

but this returns every Specific Food, while I only want the first 2 per category. So I basically need my subquery to have a limit statement in it so that it finds only the first 2 per category.

However if I simply do the following

   Select * From Food_Type join (Select * From Foods LIMIT 2) as Foods on Food_Type.Type_ID = Foods.Type_ID

My statement only returns 2 results total.

© Stack Overflow or respective owner

Related posts about sql

Related posts about database