#1111 - 在case语句mysql中使用组函数无效?

时间:2015-05-10 06:34:54

标签: php mysql sql sum data-retrieval

我想从表中检索due_date,我使用了以下查询,但它返回invalid use of group function错误。我的查询出了什么问题?

我的查询在下面给出,

   select 
sum(
    case when invoices.due_date LIKE '2015-05-10%' 
        then sum(invoice_items.quantity*invoice_items.rate)-sum(IFNULL(payments.amount,0))
    end
    )as 1day, 
sum( 
    case when invoices.due_date LIKE '2015-05-09%' 
        then sum(invoice_items.quantity*invoice_items.rate)-     sum(IFNULL(payments.amount,0))
    end
    )as yesterday 
        from `invoices`
        inner join `invoice_items` on `invoice_items`.`invoice_id` = `invoices`.`id` 
        left join `payments` on `payments`.`invoice_id` = `invoices`.`id` 
        where `invoices`.`deleted_at` is null

1 个答案:

答案 0 :(得分:2)

也许你想要这个。我在案例中移除了SUM()

select 
sum(
    case when invoices.due_date LIKE '2015-05-10%' 
        then (invoice_items.quantity*invoice_items.rate)-(IFNULL(payments.amount,0))
    end
    )as 1day, 
sum( 
    case when invoices.due_date LIKE '2015-05-09%' 
        then (invoice_items.quantity*invoice_items.rate)-     (IFNULL(payments.amount,0))
    end
    )as yesterday