gpt4 book ai didi

mysql - sql中如何获取表的最后一条记录

转载 作者:行者123 更新时间:2023-11-29 19:09:26 25 4
gpt4 key购买 nike

我的出勤表中有三个条目。但是我需要打印出勤表中 ID 的最后一个条目。我在单独的查询中计算了总持续时间,并在单独的查询中打印列表。有人请帮我解决这个问题。

打印总持续时间的查询:

select SEC_TO_TIME(sum(case when a.endtime  IS NULL 
then time_to_sec(TIMEDIFF(NOW(),a.starttime))
else time_to_sec(a.duration)
end)
) as duration
from attendance a
left join staff s on a.staffid=s.id
left join company c on a.companyid=c.companyid
where DATE_FORMAT(a.createdon, '%Y/%m/%d') = DATE_FORMAT('2017-04-04', '%Y/%m/%d') and
(a.employeecode ='A101' OR 'A101'='') and
(a.companyid=0 OR 0=0)
group by a.employeecode,a.companyid order by a.id desc

查询打印详细信息:

select a.id, 
a.starttime,
a.endtime,
a.startlocation,
a.endlocation,
a.duration,
a.companyid,
a.employeecode,
a.staffid,
a.createdon,
a.createdby,
a.lastmodifiedon,
a.reason,
a.comments,
s.name as staffName,
c.company_name as companyName
from attendance a
left joinstaff s on a.staffid=s.id
left join company c on a.companyid=c.companyid
where DATE_FORMAT(a.created_on, '%Y/%m/%d') = DATE_FORMAT('2017-04-04', '%Y/%m/%d') and
(a.employeecode ='A101' OR 'A101'='') and
(a.company_id=0 OR 0=0)
group by a.employeecode,a.companyid order by a.id desc

最佳答案

我通过将结束时间设置为空来获得输出。

`select SEC_TO_TIME(sum(case when a.endtime IS NULL then  time_to_sec(TIMEDIFF(NOW(),a.starttime)) else time_to_sec(a.duration) end)) 
from attendance a left join staff s on a.staffid=s.id
left join company c on a.companyid=c.companyid
where DATE_FORMAT(a.createdon, '%Y/%m/%d') = DATE_FORMAT('', '%Y/%m/%d') and
(a.employeecode ='' OR ''='') and
(a.companyid=0 OR 0=0)
group by a.employeecode,a.companyid order by a.id desc`

关于mysql - sql中如何获取表的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43206404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com