将tabl1的第一行的2个表连接到表2的第一行

时间:2015-02-01 08:58:05

标签: sql-server

我有2个表emptimeinemptimeoutOT。两者都有相同的字段。

table1
Emptimein
ID  Date  TimeIn

table2
ID  Date  TimeOutOT

我想加入两个表来组合emptimein表的第一行和 emptimeout

我想要一个像这样的结果

ID  Timein  TimeOutOT

2 个答案:

答案 0 :(得分:0)

案例1#

select Etimein.ID,Etimein.TimeIN,
Etimeout.TimeOutOT
from databasename.Emptimein as Etimein left outer join 
databasename.Emptimeout as Etimeout on Etimein.ID=Etimeout.ID
and Etimein.date=Etimeout.date
where Etimein.date between 'yyyy-mm-dd' and 'yyyy-mm-dd'
group by Etimein.id
order by Etimein.id
;

案例2#

select Etimein.ID,Etimein.TimeIN,
Etimeout.TimeOutOT
from databasename.Emptimein as Etimein left outer join 
databasename.Emptimeout as Etimeout on Etimein.ID=Etimeout.ID
and Etimein.date=Etimeout.date
where Etimeout.date between 'yyyy-mm-dd' and 'yyyy-mm-dd'
group by Etimein.id
order by Etimein.id
;

答案 1 :(得分:0)

您可以尝试以下查询:

SELECT EI.ID,EI.Timein,EO.TimeOutOT FROM emptimein EI 
LEFT JOIN emptimeoutOT EO ON EI.ID = EO.ID 
AND Etimein.date = Etimeout.date
WHERE Etimein.date BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd'

如果我误解了这些要求,请告诉我。