上班问题

湖中仙人 2009-05-21 09:13:27
有张上班记录表table(日期,工号,姓名,上班,下班);我想知道昨天上了班,今天没有上班;或者是昨天没有上班,今天上了班的记录怎么写SQL语句??
...全文
82 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanshichen 2009-05-21
  • 打赏
  • 举报
回复
最近老是来晚了,1~4楼都是答案,搂在自己看吧,再去找别的帖子……
youzhj 2009-05-21
  • 打赏
  • 举报
回复
学习学习
JonasFeng 2009-05-21
  • 打赏
  • 举报
回复
楼上的回答正解!
minjiaquan 2009-05-21
  • 打赏
  • 举报
回复
顶起!
wzy_love_sly 2009-05-21
  • 打赏
  • 举报
回复
--昨天上了班,今天没有上班
select * from tb t
where exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate()-1,120) and 日期<convert(varchar(10),getdate(),120))
and not exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate(),120) and 日期<convert(varchar(10),getdate()+1,120))

--昨天没有上班,今天上了班
select * from tb t
where not exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate()-1,120) and 日期<convert(varchar(10),getdate(),120))
and exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate(),120) and 日期<convert(varchar(10),getdate()+1,120))

--小F-- 2009-05-21
  • 打赏
  • 举报
回复
--昨天没有上班,今天上了班
select 工号
from [table] a
where 日期 = '昨天'
and exists (
select 1 from [table]
where 日期 = '今天'
and 工号 = a.工号
)
--或者
select 工号
from [table] a
where 日期 = '昨天'
and 工号 in(
select 工号 from [table]
)

昵称被占用了 2009-05-21
  • 打赏
  • 举报
回复
昨天上了班,今天没有上班
select 工号
from [table] a
where 日期 = '昨天'
and not exists (
select 1 from [table]
where 日期 = '今天'
and 工号 = a.工号
)

or:
select a.工号
from [table] a left join [table] b
on b.工号 = a.工号 and b.日期 = '今天'
where a.日期 = '昨天'
and b.日期 is null


昨天没有上班,今天上了班
类似,不重复了
csdyyr 2009-05-21
  • 打赏
  • 举报
回复
--1, 我想知道昨天上了班,今天没有上班
select 工号
from (
select *
from tb
where 日期=dateadd(day,-1,getdate())) a
left join
(select *
from tb
where 日期=getdate()) b
on a.工号=b.
where b.工号 is null

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧