0

这是我的 sp 查询:

  -- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`%` PROCEDURE `USP_GetUserOrders`(UserId INT)
BEGIN
SELECT op.OrderId,
    O.Number,    
    SUM(op.Price) Price,
    (SELECT CONCAT(A.Detail, ' ',C.Name, ' / ', Ci.Name) FROM kobiakinlar.Address AS A
                INNER JOIN County AS C ON C.CountyId = A.CountyId
                INNER JOIN City AS Ci ON C.CityId = Ci.CityId
                WHERE UserId = O.UserId) AS UserAddress,
   ( SELECT CASE WHEN O.Status =0 THEN 'Onay Bekliyor'  WHEN O.Status =1 THEN 'Onaylandı' WHEN O.Status = 2 THEN 'Reddedildi' END) Status,
    O.Creation,
    ( SELECT CASE WHEN O.IsDelivered =0 THEN 'Teslim Edilmedi' ELSE 'Teslim Edildi' END) IsDelivered,
    group_concat(P.Name) Product
FROM
    kobiakinlar.product P
        JOIN
    kobiakinlar.orderproduct op ON op.ProductId = P.productId
        JOIN
    kobiakinlar.order O ON O.orderId = op.OrderId
   JOIN 
kobiakinlar.address A ON A.addressId = O.AddressId
WHERE O.UserId = UserId
GROUP BY op.OrderId;
END

Error Code: 1242. Subquery returns more than 1 row当我返回时CALL USP_GetUserOrders(3)

但我只在查询选项卡中运行 sql,它运行并返回我想要的。您可以在图像中看到查询的结果:

在此处输入图像描述

你有什么建议吗?

4

1 回答 1

3

我很确定原因是 UserId 和 o.UserId 之间的混淆。

在查询上下文中,它不知道您指的是 sp 的参数。将争论的名称更改为“arg_UserId”之类的名称,并在适当的查询中替换它。

您还可以简化查询语法。case 语句的SELECT外部是多余的。此外,假设 County 和 City 的连接总是 1-1,您可以将查询重写为:

SELECT op.OrderId, O.Number,  SUM(op.Price) Price,
       CONCAT(A.Detail, ' ', C.Name, ' / ', Ci.Name) AS UserAddress,
       (CASE WHEN O.Status =0 THEN 'Onay Bekliyor'  WHEN O.Status =1 THEN 'Onaylandı' WHEN O.Status = 2 THEN 'Reddedildi' END) Status,
       O.Creation,
       (CASE WHEN O.IsDelivered =0 THEN 'Teslim Edilmedi' ELSE 'Teslim Edildi' END) IsDelivered,
       group_concat(P.Name) as Product
FROM kobiakinlar.product P JOIN
     kobiakinlar.orderproduct op
     ON op.ProductId = P.productId JOIN
     kobiakinlar.order O
     ON O.orderId = op.OrderId JOIN 
     kobiakinlar.address A ON A.addressId = O.AddressId join
     County C
     ON C.CountyId = A.CountyId join
     City AS Ci
     ON C.CityId = Ci.CityId
WHERE O.UserId = arg_UserId
GROUP BY op.OrderId;
于 2012-11-12T18:59:41.713 回答