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

标签 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

最佳答案

也许你想要这个。我把你的 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 

关于php - #1111 - 在 case 语句 mysql 中组函数的使用无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30148448/

相关文章:

php - 如何在没有连续请求的情况下创建 PHP 聊天应用程序?

php - codeigniter 使用 Ignited-Datatables 类卡住连接

sql - 仅当值不存在时才从 SELECT 插入到表中

mysql - 选择 WHERE 子句,拆分列值并检查

php - 每个 PHP-FPM Worker 是否单独加载 php.ini?

php - 如何在使用 PHP SQLSRV_QUERY 时忽略 T-SQL 打印语句

php - 在php中将数组显示为树结构

php - SELECT LAST_INSERT_ID() 在直接 MySQL 查询中有效,但在 PHP QUERY 中无效

php - 创建一个带有空白方 block 的表,用于丢失 mysql 数据

MySQL:在一条语句中合并 2 列的结果