解决SQL Server中子查询返回多个值的问题
执行以下查询时:
<code class="language-sql">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 </code>
会遇到以下错误:
<code>Msg 512, Level 16, State 1, Line 2 子查询返回多个值。当子查询位于 =、!=、<、<=、>、>= 之后,或者用作表达式时,这是不允许的。</code>
此错误是因为 cost
表达式中使用的子查询返回多个值,而使用等号运算符 (=) 进行比较时不允许这种情况。要解决这个问题,请使用显式选择 cost
列单个值的连接操作替换子查询:
<code class="language-sql">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'</code>
此查询现在将为 cost
列返回单个值,从而解决子查询错误。
以上是如何修复 SQL Server 中的'子查询返回超过 1 个值”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!