按2个表中的列进行分页和排序。
一个客户可能有多封电子邮件。
要求:获取10个客户并按last_name,电子邮件排序。
--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;
不适用于错误
ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 6: order by c.last_name, ce.email offset 0 limi...
^
PostgreSQL抱怨您的查询是正确的;你的要求不清楚。
如果您想要前10位客户,按姓氏订购电子邮件,但每位客户可以拥有多个电子邮件地址,您是如何做到的?哪个电子邮件地址用于对客户进行排序?
错误消息基本上反映了不确定性。
想出一个清晰的排序顺序定义并将其转换为SQL,然后PostgreSQL将很乐意运行您的查询。