我正在尝试从3个不同的表中创建一些列的视图。 “ OrderNumber”列之一在两个表中,因此我试图为它们做一个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;
最佳答案
替换任何您的客户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 = ?
关于mysql - MySQL错误代码1242,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39804156/