Paging and sort by columns in 2 tables.
- customer(id, first_name, last_name)
- customer_email(customer_id, type, email).
One customer could have multiple emails.
Requirement: get 10 customers and sort by last_name, email.
--Sort by customer.last_name, customer_email.email
select c.*, ce.email
from customer c
inner join (select distinct c.id, c.last_name
from customer c, customer_email ce
where c.id = ce.customer_id
order by c.last_name, ce.email offset 0 limit 10
) AS paged_c on paged_c.id = c.id
inner join customer_email ce on c.id = ce.customer_id
order by c.last_name, ce.email;
Doesn't work with the error
ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 6: order by c.last_name, ce.email offset 0 limi...
^