table a: 个人信息
(如:id,name)
如:1,a
2,b
3,c
...
table b: 公共日历
(如:id,name,date,flag--标识1:休息,2:上班)
11,元旦,2014-01-01,1
12,除夕,2014-01-30,1
...
table c:个人日历
(如:id,关联个人id,name,date,flag--标识1:休息,2:上班)
21,1,元旦上班,2014-01-01,2
22,1,元旦调休,2014-01-07,1
...
如想列出所有人1月份的休息信息? 谢谢
(如id=1的人,休息信息...)
1,2014-01-07
1,2014-01-30
...
------解决方案--------------------
试试这个:
--drop table a,b,c
create table a(id int,name varchar(10))
insert into a
select 1,'a' union all
select 2,'b' union all
select 3,'c'
create table b(id int,name varchar(10),date datetime,flag int)
insert into b
select 11,'元旦','2014-01-01',1 union all
select 12,'除夕','2014-01-30',1
create table c(id int,关联个人id int,name varchar(20),date datetime,flag int)
insert into c
select 21,1,'元旦上班','2014-01-01',2 union all
select 22,1,'元旦调休','2014-01-07',1
go
select a.id,b.date
from a
inner join b
on b.flag = 1
left join c
on a.id = c.关联个人id
and c.flag = 2
and b.date = c.date
where c.date is null
union all
select a.id,c.date
from a
inner join c
on a.id = c.关联个人id
and c.flag = 1
order by id,date
/*
id date
1 2014-01-07 00:00:00.000
1 2014-01-30 00:00:00.000
2 2014-01-01 00:00:00.000
2 2014-01-30 00:00:00.000
3 2014-01-01 00:00:00.000
3 2014-01-30 00:00:00.000
*/