Select one product per establishment in the mysql database!

1

I have a database with the tables estabelecimentos and produtos . Register the products relating to the establishments.

I need to make an appointment where I only select one product from each establishment, I thought about doing it with LIMIT but it does not seem to work.

Example: if I have 10 establishments and 100 products, I want a query that returns 10 products but one of each establishment.

    
asked by anonymous 12.01.2016 / 20:10

1 answer

3

You can use group by , it would look something like this

SELECT * FROM produto GROUP BY ID_estab

Edit

If you want to view 1 random product from each establishment

SELECT p.* FROM (SELECT * FROM produto ORDER BY RAND()) as p GROUP BY p.ID_estab
    
12.01.2016 / 20:35