[英]create view with two tables that have same columns
I am trying to create a view with the following results. 我正在尝试创建具有以下结果的视图。 I can create the first part just fine but the second part is where I am having trouble.
我可以很好地创建第一部分,但是第二部分是我遇到麻烦的地方。 When I do the join I am receiving an Error Code: 1052. Column 'customer_id' in field list is ambiguous.
当我进行联接时,我收到错误代码:1052。字段列表中的“ customer_id”列不明确。 Now I know that the in the two tables I have duplicate columns.
现在我知道在两个表中我有重复的列。
My question is how do I write the view to ignore one of the duplicate columns 我的问题是如何编写视图以忽略重复的列之一
Create a view named customer_addresses that shows the shipping and billing addresses for each customer. 创建一个名为customer_addresses的视图,该视图显示每个客户的送货地址和帐单地址。 This view should return these columns from the Customers table:
此视图应从“客户”表返回以下列:
customer_id
email_address
last_name
first_name.
This view should return these columns from the Addresses table: 此视图应从“地址”表返回这些列:
bill_line1
bill_line2
bill_city
bill_state
bill_zip
ship_line1
ship_line2
ship_city
ship_state
ship_zip
The rows in this view should be sorted by the last_name and then first_name columns. 此视图中的行应按last_name和first_name列排序。
Here is my code. 这是我的代码。
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT customer_id, email_address, last_name, first_name, addresses.line1
FROM customers JOIN addresses
ORDER BY last_name
I would just identify all the columns like this: 我只想标识所有这样的列:
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT
customers.customer_id,
customers.email_address,
customers.last_name,
customers.first_name,
addresses.line1
FROM customers JOIN addresses
ORDER BY customers.last_name
Because I believe that you have an customer_id
in the customers
table and one in the addresses
table. 因为我相信您在
customers
表中有一个customer_id
,在addresses
表中有一个。 Right? 对?
If you do not specify the table name and add a column to one of the tables with the same column name as in the view the problem will arise again. 如果您未指定表名,而是使用与视图中相同的列名将列添加到其中一个表中,则会再次出现此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.