SQL Server子查询返回多个值。 当子查询遵循=,!=,<,<=,>,> =时,这是不允许的

我运行以下查询:

SELECT orderdetails.sku, orderdetails.mf_item_number, orderdetails.qty, orderdetails.price, supplier.supplierid, supplier.suppliername, supplier.dropshipfees, cost = (SELECT supplier_item.price FROM supplier_item, orderdetails, supplier WHERE supplier_item.sku = orderdetails.sku AND supplier_item.supplierid = supplier.supplierid) FROM orderdetails, supplier, group_master WHERE invoiceid = '339740' AND orderdetails.mfr_id = supplier.supplierid AND group_master.sku = orderdetails.sku 

我得到以下错误:

消息512,级别16,状态1,行2子查询返回多个值。 当子查询遵循=,!=,<,<=,>,> =或子查询用作expression式时,这是不允许的。

有任何想法吗?

尝试这个:

 select od.Sku, od.mf_item_number, od.Qty, od.Price, s.SupplierId, s.SupplierName, s.DropShipFees, si.Price as cost from OrderDetails od inner join Supplier s on s.SupplierId = od.Mfr_ID inner join Group_Master gm on gm.Sku = od.Sku inner join Supplier_Item si on si.SKU = od.Sku and si.SupplierId = s.SupplierID where od.invoiceid = '339740' 

这将返回除cost列以外的多个相同的行。 查看返回的不同成本值,找出造成不同值的原因。 然后询问他们想要的成本价值,并将标准添加到将select该成本的查询中。

检查您正在尝试执行查询的表上是否有任何触发器。 他们有时可能会抛出这个错误,因为他们试图运行在表上的更新/select/插入触发器。

您可以修改您的查询来禁用,然后启用触发器,如果​​触发器不需要为您尝试运行的任何查询执行。

 ALTER TABLE your_table DISABLE TRIGGER [the_trigger_name] UPDATE your_table SET Gender = 'Female' WHERE (Gender = 'Male') ALTER TABLE your_table ENABLE TRIGGER [the_trigger_name] 
 cost = Select Supplier_Item.Price from Supplier_Item,orderdetails,Supplier where Supplier_Item.SKU=OrderDetails.Sku and Supplier_Item.SupplierId=Supplier.SupplierID 

此子查询返回多个值,SQL正在抱怨,因为它不能在单个logging中将多个值分配给成本。

一些想法:

  1. 修复数据,使现有的子查询只返回1条logging
  2. 修复子查询,使其仅返回一条logging
  3. 添加一个顶部1,并通过子查询sorting(讨厌的解决scheme,DBA讨厌 – 但它“起作用”)
  4. 使用用户定义的函数将子查询的结果连接成单个string
 select column from table where columns_name in ( select column from table where columns_name = 'value'); 

注意:当我们使用子查询时,我们必须关注点

  1. 如果我们的子查询在这种情况下返回一个值,我们使用(=,!=,<>,<,> ….)
  2. 其他(多个值)在这种情况下,我们正在使用(在,任何,所有,一些)

解决方法是停止使用相关子查询并使用连接。 相关的子查询本质上是游标,因为它们导致查询逐行运行,应该避免。

如果你只需要一个logging匹配,你可能需要在连接中得到一个派生表,如果你需要这两个值,那么普通join将会这样做,但是你将得到同样的多个logging编号在结果集中。 如果你只需要一个,你需要决定哪一个,在代码中,你可以使用top 1order by ,你可以使用max() ,你可以使用min()等,这取决于你的对数据的真实需求是。

select中成本部分的select语句返回多个值。 您需要在where子句中添加更多,或使用聚合。

要么你的数据不好,要么就是你的想法没有结构化。 可能两者。

为了certificate/反驳这个假设,运行这个查询:

 SELECT * from ( SELECT count(*) as c, Supplier_Item.SKU FROM Supplier_Item INNER JOIN orderdetails ON Supplier_Item.sku = orderdetails.sku INNER JOIN Supplier ON Supplier_item.supplierID = Supplier.SupplierID GROUP BY Supplier_Item.SKU ) x WHERE c > 1 ORDER BY c DESC 

如果这只返回几行,那么你的数据是不好的 。 如果它返回很多行,那么你的数据结构就不像你认为的那样。 (如果它返回零行, 我错了。

我在猜测你有多个包含相同SKU订单(两个单独的订单项,都订购相同的SKU )。

我遇到了同样的问题,我用Northwind数据库的例子来代替=

查询是:find1997年下达订单的公司

尝试这个 :

 select CompanyName from Customers where CustomerID in ( select CustomerID from Orders where year(OrderDate) = '1997'); 

而不是:

 select CompanyName from Customers where CustomerID = ( select CustomerID from Orders where year(OrderDate) = '1997'); 

错误意味着这个子查询返回多于一行:

 (Select Supplier_Item.Price from Supplier_Item,orderdetails,Supplier where Supplier_Item.SKU=OrderDetails.Sku and Supplier_Item.SupplierId=Supplier.SupplierID ) 

您可能不希望在子查询中包含orderdetails和supplier表,因为您要引用从外部查询中的表中select的值。 所以我想你想要子查询是简单的:

 (Select Supplier_Item.Price from Supplier_Item where Supplier_Item.SKU=OrderDetails.Sku and Supplier_Item.SupplierId=Supplier.SupplierID ) 

我build议你阅读关联与不关联的子查询。

正如其他人所build议的,最好的办法是使用连接而不是variables赋值。 重写你的查询来使用连接(并使用显式连接语法,而不是隐式连接,这也是最好的实践),你会得到这样的东西:

 select OrderDetails.Sku, OrderDetails.mf_item_number, OrderDetails.Qty, OrderDetails.Price, Supplier.SupplierId, Supplier.SupplierName, Supplier.DropShipFees, Supplier_Item.Price as cost from OrderDetails join Supplier on OrderDetails.Mfr_ID = Supplier.SupplierId join Group_Master on Group_Master.Sku = OrderDetails.Sku join Supplier_Item on Supplier_Item.SKU=OrderDetails.Sku and Supplier_Item.SupplierId=Supplier.SupplierID where invoiceid='339740'