如何修复MySQL查询?

时间:2016-08-03 04:30:04

标签: mysql sql

CREATE TABLE testTableOne

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 testTableTwo

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 testtableoneEmpnoNameBalancePlace)价值观(100,' John& #39;,' 1500',' Wasinton DC');

INSERT INTO testtableoneEmpnoNameBalancePlace)价值观(101,' Joselin& #39;,' 1000',' Dexcity');

INSERT INTO testtableoneEmpnoNameBalancePlace)价值观(102,' Rusfal& #39;,' 0',' Donxes'); INSERT INTO testtableoneEmpnoNameBalancePlace)VALUES(103,' Raser' ,' 100',' versity');

INSERT INTO testtableoneEmpnoNameBalancePlace)价值观(104,' && #39;,' 2500',' sew');

INSERT INTO testTableTwoDateEmpnoReceiptPaymentStatus)价值观(& #39; 2016-08-15',100,' 1000',' 0',' OK'); INSERT INTO testTableTwoDateEmpnoReceiptPaymentStatus)价值观(' 2016-08-15',100,' 0',' 1000',' OK');

INSERT INTO testTableTwoDateEmpnoReceiptPaymentStatus)价值观(& #39; 2016-08-17',101,' 0',' 2000',' OK');

INSERT INTO testTableTwoDateEmpnoReceiptPaymentStatus)价值观(& #39; 2016-08-18',103,' 100',' 0',' NOT OK'); INSERT INTO testTableTwoDateEmpnoReceiptPaymentStatus)价值观(' 2016-08-19',100,' 1500',' 0',' OK'); INSERT INTO testTableTwoDateEmpnoReceiptPaymentStatus)价值观(' 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 =余额+收付

1 个答案:

答案 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;