我正在尝试将非重复记录插入到表中但是这样做我无法插入它它给了我(0行受影响)。实际上表本身是空的,我插入新的记录。要忽略插入重复记录,我在下面添加的查询中执行此操作。
Insert Into OrderDetails (CustomerID, CustomerName, OrderID, OrderDate, ProductID, ProductName, Unit, Price)
SELECT c.CustomerID, c.CustomerName, i.OrderID, i.OrderDate, p.ProductID, p.ProductName, p.Unit, p.Price
FROM Customers c
INNER JOIN items i ON c.CustomerID=i.CustomerID
INNER JOIN itemsDetails id ON i.OrderID = id.OrderID
INNER JOIN Products P ON p.ProductID = id.ProductID
Inner Join OrderDetails od ON c.CustomerID = od.CustomerID
where c.CustomerID != od.CustomerID
我是否正确插入?
答案 0 :(得分:0)
尝试:
Insert Into OrderDetails (CustomerID, CustomerName, OrderID, OrderDate, ProductID, ProductName, Unit, Price)
SELECT c.CustomerID, c.CustomerName, i.OrderID, i.OrderDate, p.ProductID, p.ProductName, p.Unit, p.Price
FROM Customers c
INNER JOIN items i ON c.CustomerID=i.CustomerID
INNER JOIN itemsDetails id ON i.OrderID = id.OrderID
INNER JOIN Products P ON p.ProductID = id.ProductID
where c.CustomerID NOT IN (SELECT customerid FROM orderdetails)
答案 1 :(得分:0)
错误在于语句的最后两行:
Inner Join OrderDetails od ON c.CustomerID = od.CustomerID
where c.CustomerID != od.CustomerID
在内部联接中,您需要CustomerID相同。 在where子句中,您需要CustomerID不同。
删除表OrderDetails的内连接。