【问题标题】:Why am I receiving the error 1242: Subquery returns more than 1 row? [closed]为什么我会收到错误 1242:子查询返回多于 1 行? [关闭]
【发布时间】:2023-02-08 21:00:45
【问题描述】:
SELECT *,
  IFNULL((SELECT      
        Tenant.ServiceUseage.Useage 
        FROM 
                Tenant.ServiceUseage
                LEFT JOIN Tenant.ServiceSubscription
                ON Tenant.ServiceSubscription.ServiceMatricsID = Tenant.ServiceUseage.ServiceMatricsID
                WHERE
                (Tenant.ServiceUseage.Date >= Tenant.ServiceSubscription.Date 
                AND
                (Tenant.ServiceUseage.Date <= Tenant.ServiceSubscription.Date_Of_Inactive
        OR
            Tenant.ServiceSubscription.Date_Of_Inactive IS NULL))
                AND USEAGE.SERVICE_MATRICS_ID = Tenant.ServiceUseage.ServiceMatricsID
                AND MONTH(USEAGE.Useage_Date) = MONTH(DATE_ADD(Tenant.ServiceUseage.Date, INTERVAL 1       
                MONTH))), 0) AS PREVIOUS_Useage
FROM
(SELECT 
    Tenant.ServiceUseage.ID AS USEAGE_ID,
    Tenant.Tenant.ID AS TENANT_ID,
    Tenant.Tenant.Name,
    Tenant.RentalUnit.ID AS RENTAL_UNIT_ID,
    Tenant.RentalUnit.Type AS RENTAL_TYPE,
    Tenant.ServiceMatrics.ID AS SERVICE_MATRICS_ID,
    Tenant.ServiceUseage.Date AS Useage_Date,
    Tenant.ServiceUseage.Useage
FROM Tenant.RentalUnit
LEFT JOIN Tenant.RentalUnitSubscription
ON Tenant.RentalUnit.ID = Tenant.RentalUnitSubscription.RentalUnitID
LEFT JOIN Tenant.Tenant
ON Tenant.Tenant.ID = Tenant.RentalUnitSubscription.TenantID
LEFT JOIN Tenant.ServiceSubscription
ON Tenant.Tenant.ID = Tenant.ServiceSubscription.TenantID
LEFT JOIN Tenant.Service
ON Tenant.ServiceSubscription.ServiceID = Tenant.Service.ID
LEFT JOIN Tenant.ServiceMatrics
ON Tenant.ServiceSubscription.ServiceMatricsID = Tenant.ServiceMatrics.ID
LEFT JOIN Tenant.ServiceUseage
ON Tenant.ServiceSubscription.ServiceMatricsID = Tenant.ServiceUseage.ServiceMatricsID
WHERE
        Tenant.ServiceUseage.Date >= Tenant.ServiceSubscription.Date 
                AND
                (Tenant.ServiceUseage.Date <= Tenant.ServiceSubscription.Date_Of_Inactive
                OR
        Tenant.ServiceSubscription.Date_Of_Inactive IS NULL)               
ORDER BY 
    Tenant.ServiceUseage.ID
    ) AS USEAGE
;

【问题讨论】:

  • 为什么你标记了两个 SQL ServerMySQL?你是什​​么产品实际上使用?我删除了冲突的标签/措辞;您需要edit问题以适当地(重新)标记。
  • 我还删除了您问题中的文字;将问题向导中的文本复制到您的问题中没有帮助或有用。这导致您的“问题”只是代码。因此,您还需要在问题中添加一些实际内容;代码转储不是问题。
  • 错误消息告诉你问题所在:IFNULL()(一个 MySQL 函数)内的子查询必须只返回一行。
  • 错误代码:1242。子查询返回多于 1 行

标签: sql


【解决方案1】:

错误1242: Subquery returns more than 1 row 是由IFNULL语句中的子查询引起的。这是因为子查询返回多行,这在 IFNULL 语句中是不允许的。要解决此问题,您可以使用 SUM 或 AVG 等聚合函数从子查询返回单个值。

SELECT *,
  IFNULL((SELECT      
        SUM(Tenant.ServiceUseage.Useage) 
        FROM 
                Tenant.ServiceUseage
                LEFT JOIN Tenant.ServiceSubscription
                ON Tenant.ServiceSubscription.ServiceMatricsID = Tenant.ServiceUseage.ServiceMatricsID
                WHERE
                (Tenant.ServiceUseage.Date >= Tenant.ServiceSubscription.Date 
                AND
                (Tenant.ServiceUseage.Date <= Tenant.ServiceSubscription.Date_Of_Inactive
        OR
            Tenant.ServiceSubscription.Date_Of_Inactive IS NULL))
                AND USEAGE.SERVICE_MATRICS_ID = Tenant.ServiceUseage


 

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多