Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Begrenzen Sie die Anzahl der Zeilen pro ID in einer SQL-Abfrage

Anpassung von einer anderen Antwort

  set @brand_id= '';
  set @num = 0;

  select
  products.id, products.brand_id,
  @num := if(@brand_id= brand_id, @num + 1, 1) as dummy_1,
  @brand_id:= brand_id as dummy_2,
  @num as row_number
  from products
  where brand_id IN ('1', '2','3')
  group by
  brand_id,
  row_number
  having row_number <= 10;