我正在尝试从3个不同的表创建一些列的视图。其中一个列'OrderNumber'在2个表中,所以我试图为它们做一个UNION,但因为我做了一个子查询,它返回一个1242错误,并且不会返回超过1行。我只是想知道如何重写这个查询,以便没有子查询,或者有什么方法可以绕过它。或许我需要写多个查询?虽然我更愿意将其保留在一个查询中,谢谢。
CREATE VIEW CustOrderItems AS
SELECT CustFirstName,
CustLastName,
(SELECT OrderNumber
FROM Orders
UNION
SELECT OrderNumber
FROM Order_Details)
OrderDate,
ShipDate,
QuantityOrdered * QuotedPrice as ItemTotal
FROM Customers JOIN Orders JOIN Order_Details;
答案 0 :(得分:0)
目前尚不清楚你的连接标准是什么,因为语句语法不好。但我假设您想加入OrderNumber,例如SELECT ... FROM Customers INNER JOIN Orders ON Customers.OrderNumber = Orders.OrderNumber。在这种情况下,如果您想使用两个表中的订单号,只需重复查询并将两者结合起来,如: SELECT ,,, FROM Customers INNER JOIN Order_Details ON Customers.OrderNumber = Order_Details.OrderNumber 联盟 从客户INNER JOIN订单中选择Customers.OrderNumber = Orders.OrderNumber
答案 1 :(得分:0)
替换您的客户ID
drop view if exists custorders;
create view custorders as
SELECT c.CustFirstName,
c.CustLastName,
o.OrderNumber order_ordernumber,
od.OrderNumber orderdetails_ordernumber,
o.OrderDate,
o.ShipDate,
od.QuantityOrdered * od.QuotedPrice as ItemTotal
FROM Customers c
JOIN Orders o on c.id = o.cust_id
JOIN Order_Details od on o.ordernumber = od.ordernumber
where c.id = ?