CREATE TABLE test
。TableOne
(
Id
INT NOT NULL AUTO_INCREMENT,
Empno
INT NULL,
Name
VARCHAR(45)NULL,
Balance
DECIMAL(6,2)NULL,
Place
VARCHAR(45)NULL,
PRIMARY KEY(Id
));
CREATE TABLE test
。TableTwo
(
Id
INT NOT NULL AUTO_INCREMENT,
Date
DATE NULL,
Empno
INT NULL,
Receipt
DECIMAL(6,2)NULL,
Payment
DECIMAL(6,2)NULL,
Status
VARCHAR(45)NULL,
PRIMARY KEY(Id
));
INSERT INTO test
。tableone
(Empno
,Name
,Balance
,Place
)价值观(100,' John& #39;,' 1500',' Wasinton DC');
INSERT INTO test
。tableone
(Empno
,Name
,Balance
,Place
)价值观(101,' Joselin& #39;,' 1000',' Dexcity');
INSERT INTO test
。tableone
(Empno
,Name
,Balance
,Place
)价值观(102,' Rusfal& #39;,' 0',' Donxes');
INSERT INTO test
。tableone
(Empno
,Name
,Balance
,Place
)VALUES(103,' Raser' ,' 100',' versity');
INSERT INTO test
。tableone
(Empno
,Name
,Balance
,Place
)价值观(104,' && #39;,' 2500',' sew');
INSERT INTO test
。TableTwo
(Date
,Empno
,Receipt
,Payment
,Status
)价值观(& #39; 2016-08-15',100,' 1000',' 0',' OK');
INSERT INTO test
。TableTwo
(Date
,Empno
,Receipt
,Payment
,Status
)价值观(' 2016-08-15',100,' 0',' 1000',' OK');
INSERT INTO test
。TableTwo
(Date
,Empno
,Receipt
,Payment
,Status
)价值观(& #39; 2016-08-17',101,' 0',' 2000',' OK');
INSERT INTO test
。TableTwo
(Date
,Empno
,Receipt
,Payment
,Status
)价值观(& #39; 2016-08-18',103,' 100',' 0',' NOT OK');
INSERT INTO test
。TableTwo
(Date
,Empno
,Receipt
,Payment
,Status
)价值观(' 2016-08-19',100,' 1500',' 0',' OK');
INSERT INTO test
。TableTwo
(Date
,Empno
,Receipt
,Payment
,Status
)价值观(' 2016-08-20',100,' 0',' 1000',' OK');
然后我需要输出这个
开始日期' 2016-08-18'结束日期' 2016-08-20'
Empno名称放置OB CB
100 John Wasinton DC 2400 2000
101 Joselin Dexcity 1000 0
102 Rusfal Donxes 0 0
等 103 104
也
条件 根据状态OK
,OB少于开始日期forumula OB =余额+收付
条件 根据状态OK
,从日期到日期进行CB计算forumula CB =余额+收付
答案 0 :(得分:1)
使用此:
SELECT a.id,a.name,sum(amount1)as amount1,sum(Receipt) as Receipt,sum(payment) as payment,
sum(amount1)+sum(Receipt)-sum(payment) as Total,nameid,b.name
FROM test.mas as a left join test.trans as b on a.id=b.nameid GROUP BY a.id;