Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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_Select_Balance - Fatal编程技术网

Mysql 解决这个问题的办法是什么?

Mysql 解决这个问题的办法是什么?,mysql,sql,select,balance,Mysql,Sql,Select,Balance,我需要得到如上所示的期末余额。给出您的数据 select opening_balances.branch_ID, opening_balances.comm_ID, (ifnull(comm_issue.comm_issue_quantity,0) - ifnull(comm_sales.comm_sale_quantity,0)) AS net, (opening_balances.comm_quantity + net) AS Closing_Balance FROM opening

我需要得到如上所示的期末余额。

给出您的数据

select opening_balances.branch_ID, opening_balances.comm_ID, 
 (ifnull(comm_issue.comm_issue_quantity,0) - ifnull(comm_sales.comm_sale_quantity,0)) AS net,
 (opening_balances.comm_quantity + net) AS Closing_Balance
 FROM opening_balances
JOIN comm_sales JOIN comm_issue
ON opening_balances.branch_ID = ifnull(comm_sales.branch_ID,0) = ifnull(comm_issue.branch_ID,0) = opening_balances.branch_ID 
&& opening_balances.comm_ID = ifnull(comm_sales.comm_ID,0) = opening_balances.comm_ID = ifnull(comm_issue.comm_ID,0) ;
您的查询似乎试图按分支和comm_id获取一些总计。如果是这种情况,我希望看到一些聚合函数(sum)和group by。也许是这样

drop table if exists opening_balances,comm_sales,comm_issue;


create table Opening_balances 
(`Date` date, branch_ID int, comm_ID int, comm_quantity int);
insert into opening_balances values
 ('2017-11-01',1,1,5), ('2017-11-01',1,2,6), ('2017-11-01',2,1,7), ('2017-11-01',2,2,8),
 ('2017-11-01',3,2,10);

create table comm_sales 
(branch_ID int, comm_ID int, comm_sale_quantity int, `Date` datetime);
insert into comm_sales values
 (1,1,3,'2017-10-19 11:03:15'), (1,2,3,'2017-10-19 11:03:30'), (2,1,3,'2017-10-19 11:03:37'), (2,2,4,'2017-10-19 11:03:42');

create table comm_issue 
(branch_ID int, comm_ID int, comm_issue_quantity int, `Date` datetime);
insert into comm_issue values
 (1,1,3,'2017-10-21 04:37:05');
如果您想得到小计,可以使用rollup添加到查询的末尾

select ob.branch_id, ob.comm_id,
         coalesce(sum(ob.comm_quantity),0) OpeningQTY,
         Coalesce(sum(ci.comm_issue_quantity),0) IssueQty,
         coalesce(sum(cs.comm_sale_quantity),0) SaleQty,
         coalesce(sum(ci.comm_issue_quantity),0) - coalesce(sum(cs.comm_sale_quantity),0) net,
         coalesce(sum(ob.comm_quantity),0)  +
         coalesce(sum(ci.comm_issue_quantity),0) - 
         coalesce(sum(cs.comm_sale_quantity),0) ClosingQty
from opening_balances ob
left join comm_sales cs on cs.branch_id = ob.branch_id and cs.comm_id = ob.comm_id
left join comm_issue ci on ci.branch_id = ob.branch_id and ci.comm_id = ob.comm_id
group by ob.branch_id , ob.comm_id 

+-----------+---------+------------+----------+---------+------+------------+
| branch_id | comm_id | OpeningQTY | IssueQty | SaleQty | net  | ClosingQty |
+-----------+---------+------------+----------+---------+------+------------+
|         1 |       1 |          5 |        3 |       3 |    0 |          5 |
|         1 |       2 |          6 |        0 |       3 |   -3 |          3 |
|         2 |       1 |          7 |        0 |       3 |   -3 |          4 |
|         2 |       2 |          8 |        0 |       4 |   -4 |          4 |
|         3 |       2 |         10 |        0 |       0 |    0 |         10 |
+-----------+---------+------------+----------+---------+------+------------+
5 rows in set (0.02 sec)

请解释一下。找到一个不是无用的标题。阅读并付诸行动,&。首先修复联接-语法是join table on…其次,我们无法知道这些表是聚合的还是查询需要聚合函数-将样本数据和预期结果作为文本发布或发送到SQLFIDLE。第三,我们无法告诉您是否需要期初余额的所有内容,即使没有销售额和/或issues.@P.MySQL允许(内部)连接而不启用,这意味着交叉连接。MASHFERDISTA阅读了关于在与所联接的表对相关的条件下使用良好样式的文章。而且你是misuing=,你不能把它们排成一行,表示一个独立等式的结合。@philipxy,我知道-在我看来很糟糕view@P.Salmon我不介意,因为
内部连接
只是
交叉连接
只是
交叉连接
具有更松散的绑定&上的
只是
中的
具有更紧密的绑定<只有
外部联接
s才需要启用code>on。
    select ob.branch_id, ob.comm_id,
             coalesce(sum(ob.comm_quantity),0) OpeningQTY,
             Coalesce(sum(ci.comm_issue_quantity),0) IssueQty,
             coalesce(sum(cs.comm_sale_quantity),0) SaleQty,
             coalesce(sum(ci.comm_issue_quantity),0) - coalesce(sum(cs.comm_sale_quantity),0) net,
             coalesce(sum(ob.comm_quantity),0)  +
             coalesce(sum(ci.comm_issue_quantity),0) - 
             coalesce(sum(cs.comm_sale_quantity),0) ClosingQty
    from opening_balances ob
    left join comm_sales cs on cs.branch_id = ob.branch_id and cs.comm_id = ob.comm_id
    left join comm_issue ci on ci.branch_id = ob.branch_id and ci.comm_id = ob.comm_id
    group by ob.branch_id , ob.comm_id with rollup;

+-----------+---------+------------+----------+---------+------+------------+
| branch_id | comm_id | OpeningQTY | IssueQty | SaleQty | net  | ClosingQty |
+-----------+---------+------------+----------+---------+------+------------+
|         1 |       1 |          5 |        3 |       3 |    0 |          5 |
|         1 |       2 |          6 |        0 |       3 |   -3 |          3 |
|         1 |    NULL |         11 |        3 |       6 |   -3 |          8 |
|         2 |       1 |          7 |        0 |       3 |   -3 |          4 |
|         2 |       2 |          8 |        0 |       4 |   -4 |          4 |
|         2 |    NULL |         15 |        0 |       7 |   -7 |          8 |
|         3 |       2 |         10 |        0 |       0 |    0 |         10 |
|         3 |    NULL |         10 |        0 |       0 |    0 |         10 |
|      NULL |    NULL |         36 |        3 |      13 |  -10 |         26 |
+-----------+---------+------------+----------+---------+------+------------+
9 rows in set (0.00 sec)