使用mysql上的视图组合来自多个表的数据

时间:2013-05-07 00:20:57

标签: mysql views

我有两张表EmployeeCustomer

我想要Employee的名称和地址,然后在一个视图中找到Customer的名称和地址。

这就是我所拥有的:

CREATE VIEW Mail_List AS
SELECT C.CustName, C.CustAddress
FROM Customers C
UNION
Select E.EmpCustName, E.EmpCustAddress
From Employees E;

但它说No rows were affected。请帮忙!

1 个答案:

答案 0 :(得分:1)

No rows were affected不是SELECT操作应出现的错误。

你是怎么称呼这个观点的?试着这样做:

SELECT * FROM Mail_List;

为清楚起见,您可以将视图重写为:

CREATE OR REPLACE VIEW Mail_List AS
SELECT C.CustName AS name, C.CustAddress AS address, 'customer' AS `type`
FROM Customers C
UNION ALL
SELECT E.EmpCustName AS name, E.EmpCustAddress AS address, 'employee' AS `type`
From Employees E
ORDER BY name;