Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql查询帮助_Mysql_Sql_Mysql Error 1111 - Fatal编程技术网

mysql查询帮助

mysql查询帮助,mysql,sql,mysql-error-1111,Mysql,Sql,Mysql Error 1111,我有一个数据库,里面有我在线网店的所有交易记录,我正试图通过查询打印出一份简单的财务报表 它将打印在如下表格中: <th>month</th> <th>number of sales</th> <th>money in</th> <th>money out</th> <th>result</th> 有人能给我指出正确的方向吗?选择 SELECT month(transact

我有一个数据库,里面有我在线网店的所有交易记录,我正试图通过查询打印出一份简单的财务报表

它将打印在如下表格中:

<th>month</th>
<th>number of sales</th>
<th>money in</th>
<th>money out</th>
<th>result</th>
有人能给我指出正确的方向吗?

选择
SELECT 
month(transaction_date) as month,
sum(if(incoming_amount>0,1,0)) as number_of_sales,
sum(incoming_amount)/1.25 as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount/1.25)-outgoing_amount) as result
FROM myDB 
WHERE timestamp>='2011-01-01 00:00:00' AND timestamp<='2011-12-11 23:59:59'
GROUP BY month;
月份(交易日)作为月份, 金额(如果(收入金额>0,1,0))作为销售数量, 金额(传入金额)/1.25作为货币输入, 金额(流出金额)为流出金额, 作为结果的金额((传入金额/1.25)-传出金额) 来自myDB 其中时间戳>='2011-01-01 00:00:00'和时间戳'0')
不正确
  • sum
    看起来也不正确
  • 选择
    月份(交易日)作为月份,
    金额(如果(收入金额>0,1,0))作为销售数量,
    金额(传入金额)/1.25作为货币输入,
    金额(流出金额)为流出金额,
    作为结果的金额((传入金额/1.25)-传出金额)
    来自myDB
    其中时间戳>='2011-01-01 00:00:00'和时间戳'0')
    不正确
  • sum
    看起来也不正确

  • 添加group by语句:

    SELECT 
    month(transaction_date) as month,
    count(incoming_amount > '0') as number_of_sales,
    sum(incoming_amount / 1.25) as money_in,
    sum(outgoing_amount) as money_out,
    sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
    FROM myDB WHERE year(timestamp) = '2011' GROUP BY month order by id desc");
    

    添加group by语句:

    SELECT 
    month(transaction_date) as month,
    count(incoming_amount > '0') as number_of_sales,
    sum(incoming_amount / 1.25) as money_in,
    sum(outgoing_amount) as money_out,
    sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
    FROM myDB WHERE year(timestamp) = '2011' GROUP BY month order by id desc");
    

    基于@ajreal的答案,您可以通过重用以前计算的值来加速此查询,如下所示:

    SELECT s.*,
           (s.money_in - s.money_out) as result 
    FROM
      (
      SELECT 
        month(transaction_date) as month,
        /*  year(transaction_date) as year   */  
        sum(incoming_amount>0) as number_of_sales, -- true = 1, false = 0.
        sum(incoming_amount)/1.25 as money_in,
        sum(outgoing_amount) as money_out,
      FROM myDB 
      WHERE transaction_date BETWEEN '2011-01-01 00:00:00' AND '2011-12-31 23:59:59'
      GROUP BY /*year,*/ month DESC;
      ) AS s
    
    如果选择“年度以外”,请取消对相关章节的注释。

    注意:您可以将
    DESC
    修饰符添加到
    groupby
    以首先获得最新的结果。

    基于@ajreal的答案,您可以通过重用以前计算的值来加快查询速度,如下所示:

    SELECT s.*,
           (s.money_in - s.money_out) as result 
    FROM
      (
      SELECT 
        month(transaction_date) as month,
        /*  year(transaction_date) as year   */  
        sum(incoming_amount>0) as number_of_sales, -- true = 1, false = 0.
        sum(incoming_amount)/1.25 as money_in,
        sum(outgoing_amount) as money_out,
      FROM myDB 
      WHERE transaction_date BETWEEN '2011-01-01 00:00:00' AND '2011-12-31 23:59:59'
      GROUP BY /*year,*/ month DESC;
      ) AS s
    
    如果选择“年度以外”,请取消对相关章节的注释。

    注:您可以将
    DESC
    修饰符添加到
    groupby
    以首先获得最新的结果。

    您真的有一个名为
    myDB
    的表吗?您真的有一个名为
    myDB
    的表吗?