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

问题描述

试试这个:

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 
   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子查询返回的值大于1。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,这是不允许的。

有任何想法吗?