Limitar resultados (limit)

-- El cliente que más gasta

select customer.*, sum(amount) gasto 
from customer 
join payment on customer.customer_id=payment.customer_id
group by customer.customer_id
order by gasto desc
limit 1; 

-- el segundo que más gasta

select customer.*, sum(amount) gasto 
from customer 
join payment on customer.customer_id=payment.customer_id
group by customer.customer_id
order by gasto desc
limit 1,1; 

-- Paginación: limit 0,10   limit 10,10     limit 20,10
-- Se suele hacer desde el programa

select customer.*, sum(amount) gasto 
from customer 
join payment on customer.customer_id=payment.customer_id
group by customer.customer_id
order by gasto desc
limit 0,10; 

select customer.*, sum(amount) gasto 
from customer 
join payment on customer.customer_id=payment.customer_id
group by customer.customer_id
order by gasto desc
limit 10,10; 

select customer.*, sum(amount) gasto 
from customer 
join payment on customer.customer_id=payment.customer_id
group by customer.customer_id
order by gasto desc
limit 20,10; 

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos