我的查询有问题,我在调用ob和emp_data表下面有一张表截图
我想基于obCo = 5获得liv字段的总和,而基于obCo = 6获得相同字段的最新总和。这是我的查询。
select ob.obNo,
SUM(ob.date) as liv,
ob.empId,
ob.obDesc,
ob.lugar,
ob.sponsor,
ob.dFile,
ob.obCo,
emp_data.lname,
emp_data.fname
from emp_data
inner join ob on ob.empId=emp_data.emp_id
where obCo=5
and dFile >= '$year/05/01'
and dFile <='$nyear/04/31' and ak_id=1
group by ob.empId
order by lname ASC
最佳答案
对一个IF()
表达式求和。
select ob.obNo,
SUM(if (obCo = 5, ob.date, 0)) as liv,
SUM(if (obCo = 6, ob.date, 0)) as late,
ob.empId,
ob.obDesc,
ob.lugar,
ob.sponsor,
ob.dFile,
ob.obCo,
emp_data.lname,
emp_data.fname
from emp_data
inner join ob on ob.empId=emp_data.emp_id
where obCo IN (5, 6)
and dFile >= '$year/05/01'
and dFile <='$nyear/04/31' and ak_id=1
group by ob.empId
order by lname ASC
SQLFIDDLE DEMO
关于mysql - 如何对字段求和并使用不同的ID创建两个不同的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18888766/