我必须创建一个视图来显示数据库中订单的概述。概述不需要订单商品,只需订单ID,订单日期,客户名称,订单总数和订单状态。在数据库中,有2个表包含客户详细信息(一个用于注册用户,一个用于未注册用户),shop_customer和shop_temp_customer。
我需要视图从相关表中提取客户的姓名和姓氏,以便我可以在订单概览页面上显示它。
以下是表结构:
shop_orders:
Column Name Column Type Null Default
order_id int(11) No None
order_date int(255) No None
order_customer_id int(11) Yes NULL
order_temp_customer_id int(11) Yes NULL
order_postage double(11,2) No None
order_discount double(11,2) No None
order_total double(11,2) No None
order_status tinyint(1) No None
shop_customers:
Column Name Column Type Null Default
customer_id int(11) No None
customer_title varchar(255) No None
customer_forename varchar(255) No None
customer_surname varchar(255) No None
customer_company varchar(255) No None
customer_contact_number varchar(255) No None
customer_email varchar(255) No None
customer_password varchar(255) No None
customer_reference varchar(255) No None
customer_marketing tinyint(1) No None
customer_active tinyint(1) No None
customer_decomission_date int(11) No None
shop_temp_customers:
Column Name Column Type Null Default
temp_customer_id int(11) No None
temp_customer_title varchar(255) No None
temp_customer_forename varchar(255) No None
temp_customer_surname varchar(255) No None
temp_customer_company varchar(255) No None
temp_customer_contact_number varchar(255) No None
temp_customer_email varchar(255) No None
temp_customer_reference varchar(255) No None
temp_customer_decomission_date int(11) No None
以下是我的视图代码,但它没有提取任何数据项:
CREATE VIEW shop_order_view_all_preview AS
SELECT o.order_id, o.order_date, c.customer_forename, c.customer_surname, tc.temp_customer_forename, tc.temp_customer_surname, o.order_total, o.order_status
FROM shop_orders o
RIGHT JOIN shop_customers c
ON c.customer_id = o.order_customer_id
RIGHT JOIN shop_temp_customers tc
ON tc.temp_customer_id = o.order_customer_id
有什么想法吗?
编辑:
以下是表格中的一些数据:
shop_orders:
order_id order_date order_customer_id order_temp_customer_id order_postage order_discount order_total order_status
1 1322697540 2 NULL 12.50 0.00 1012.50 0
2 1322697540 NULL 1 13.00 0.00 1200.00 1
shop_customer:
customer_id customer_title customer_forename customer_surname customer_company customer_telephone customer_email customer_password customer_reference customer_marketing customer_active customer_decomission_date
2 Mr b a d 044444 a@test.com 098f6bcd4621d373cade4e832627b4f6 Poster / Flyer 1 1 0
shop_temp_customer:
temp_customer_id temp_customer_title temp_customer_forename temp_customer_surname temp_customer_company temp_customer_contact_number temp_customer_email temp_customer_reference temp_customer_decomission_date
1 Mr e asy NULL 04444 test@test.com Google 1322697540
答案 0 :(得分:3)
尝试使用左连接而不是右连接,因为包含所有订单的表位于左侧:
CREATE VIEW shop_order_view_all_preview
AS
SELECT
o.order_id,
o.order_date,
c.customer_forename,
c.customer_surname,
tc.temp_customer_forename,
tc.temp_customer_surname,
o.order_total,
o.order_status
FROM shop_orders o
LEFT JOIN shop_customers c ON c.customer_id = o.order_customer_id
LEFT JOIN shop_temp_customers tc ON tc.temp_customer_id = o.order_customer_id
这可能无法解决问题,但我会先尝试一下。
答案 1 :(得分:2)
最后一行的一个小问题:
CREATE VIEW shop_order_view_all_preview AS
SELECT o.order_id, o.order_date, c.customer_forename, c.customer_surname, tc.temp_customer_forename, tc.temp_customer_surname, o.order_total, o.order_status
FROM shop_orders o
LEFT JOIN shop_customers c
ON c.customer_id = o.order_customer_id
LEFT JOIN shop_temp_customers tc
ON tc.temp_customer_id = o.order_temp_customer_id
-- ^^^^^
- 您将订单表中的永久 customer_id链接到临时客户表,即使订单表上有单独的字段用于临时客户ID。
编辑:将右连接更改为左连接 - 订单表应位于连接的内。
答案 2 :(得分:0)
试试这个......
CREATE VIEW shop_order_view_all_preview AS
SELECT
shop_orders.order_id, shop_orders.order_date, shop_customers.customer_forename, shop_customers.customer_surname, shop_temp_customers.temp_customer_forename, shop_temp_customers.temp_customer_surname, shop_orders.order_total, shop_orders.order_status
FROM
shop_orders, shop_customers, shop_temp_customers
RIGHT JOIN shop_customers
ON customer_id = order_customer_id
AND
RIGHT JOIN shop_temp_customers
ON temp_customer_id = order_customer_id