Group by only for "limit and offset" in MySQL

database, mysql, sql

Solution

It is clear. Try this:

select * from t t1
join (
  select distinct carBrand from t
  limit 2
) s on t1.carBrand = s.carBrand

Before the `limit 2` apply the ordering you want.

Problem

Whilst trying to do pagination I've run into this problem. My table- ``` ID CarBrand Car Model --------------------------- 1 Alfa Romeo Guilietta 2 Alfa Romeo Mito 3 Audi A3 4 Audi R8 5 Audi TT 6 Fiat Punto 7 Fiat Panda 8 Ford Mondeo 9 Ford Mustang 10 Nissan Almera 11 Nissan Note 12 Nissan Qashqai 13 Toyota Aygo 14 Toyota Prius 15 Volkswagen Beetle 16 Volkswagen Golf 17 Volkswagen Polo 18 Volkswagen Up ``` I have the data displayed like so, in groups of two: ``` -Fiat - Punto Panda -Ford - Mondeo Mustang ``` So there are 2 brands but 4 database results. Is it possible to have a query limit and offset my results to two brands while showing all the models for the brand? Sorry if I'm not clear!

Original source

Related problems