Viewを作成する
CREATE OR REPLACEVIEW v_customer_orders AS SELECT
customer_id,
MAX(order_date) last_order_date,
SUM(sub_total + tax) total_price
FROM t_orders
GROUP BY customer_id;
顧客テーブルと作成したViewを結合して顧客
氏名 最終購入日 購入金額合計を取得する
SELECT
c.first_name,
c.last_name,
IFNULL(co.last_order_date,'-'),
IFNULL(co.total_price,'-')
FROM t_customers c
LEFT JOIN v_customer_orders co ON
c.id = co.customer_id;
41.
Viewに項目を追加する
CREATE OR REPLACEVIEW v_customer_orders AS SELECT
customer_id,
MAX(order_date) last_order_date,
MIN(order_date) first_order_date,
COUNT(customer_id) order_count,
SUM(sub_total + tax) total_price
FROM t_orders
GROUP BY customer_id;