mysql - 如何在 MySql 中连接两个表时创建 View

标签 mysql join view

我需要创建一个名为 customer_addresses 的 View ,以显示每个客户的送货地址和账单地址。此 View 应返回 Customers 表中的这些列:

customer_id  
email_address  
last_name  
first_name.

这个 View 应该从 Addresses 表中返回这些列:

bill_line1  
bill_line2  
bill_city  
bill_state  
bill_zip  
ship_line1  
ship_line2  
ship_city  
ship_state  
ship_zip  

此 View 中的行应按 last_name 列排序,然后按 first_name 列排序。

注意事项
customers 表包括以下列:customer_id、email_address、password、first_name、last_name、shipping_address_id 和 billing_address_id

Addresses 表包括以下列:address_id、customer_id、line1、line2、city、state、zip_code、phone

我试图张贴两张 table 的照片,但我是新来的,还没有 10 个代表。由于将 billing_id/shipping_id 转换为实际地址,我主要在加入声明中遇到问题。

最佳答案

加入“地址”表两次,一次链接到账单地址 ID,另一次链接到送货地址 ID。

CREATE VIEW CUSTOMER_ADDRESSES AS 
SELECT cust.customer_id, cust.last_name, cust.first_name, cust.email_address
bill.line1 as bill_line1, bill.line2 as bill_line2, bill.city as bill_city, 
bill.state as bill_state, bill.zip as bill_zip, 
ship.line1 as ship_line1, ship.line2 as ship_line2, ship.city as ship_city, 
ship.state as ship_state, ship.zip as ship_zip 
FROM customers cust, addresses bill, addresses ship 
WHERE cust.shipping_address_id = ship.address_id 
and cust.billing_address_id = bill.address_id

关于mysql - 如何在 MySql 中连接两个表时创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19612317/

相关文章:

mysql - 无法将 a 插入 MySQL 中的 DATETIME 列

php - 将所有字段选择到具有相同 id_ref 的两个不同表中

MySql 根据第一个和最后一个关系之间的差异检索行

mysql - 从 ACTUAL 值左连接 MIS-MATCH

Java执行多条SQL语句并写入excel

MYSQL:带条件的输出列表

r - 使用 R 更正/整理数据

email - Express with EJS 能否将 HTML 呈现为变量(以便我可以作为电子邮件发送)?

Android:小部件和 View 之间有真正的区别吗?

ios - View 未显示?