我的数据库中有两个表(及其列):
CUSTOMERS(ID, FIRSTNAME, LASTNAME, ADDRESS);
ORDERS (ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER DATE, ID_CUSTOMER, AMOUNT);
这是我应该怎么做:
请进行一系列选择,然后按FIRSTNAME和LASTNAME进行排序。
答案 0 :(得分:1)
您可以通过对customers
表的汇总查询将orders
表联接起来:
SELECT firstname, lastname, num_orders, sum_orders
FROM customers
JOIN (SELECT id_customer, COUNT(*) AS num_orders, SUM(amount) AS sum_orders
FROM orders
GROUP BY id_customer) OR id_customer = id
ORDER BY 1, 2