两个表具有相同的复合键

时间:2014-02-12 09:30:16

标签: mysql sql tsql plsql

我有两张带有相同复合键的桌子。 以下是表格

表T1

No  |  Date        |  S_hour
----+---------------+-------
1   |  2012-01-01  |  8
1   |  2012-01-02  |  8
1   |  2012-01-03  |  8
2   |  2012-01-01  |  8
2   |  2012-01-02  |  8
2   |  2012-01-03  |  8

表T2

No  |  Date        |  L_hour
----+---------------+-------
1   |  2012-01-01  |  8
1   |  2012-01-02  |  3
1   |  2012-01-03  |  8
2   |  2012-01-01  |  2
2   |  2012-01-02  |  8
2   |  2012-01-03  |  8

S_hour是预定的小时,L_hour是休假时间。

在我的查询输出中我想要S_hour - L_hour.Following应该是查询输出 查询输出

No  |  Date        |  S_hour - L_hour
----+---------------+-------
1   |  2012-01-01  |  0
1   |  2012-01-02  |  5
1   |  2012-01-03  |  0
2   |  2012-01-01  |  6
2   |  2012-01-02  |  0
2   |  2012-01-03  |  0

提前致谢

3 个答案:

答案 0 :(得分:2)

select  t1.No
,       t1.Date
,       t1.S_hour - t2.L_hour
from    Table1 t1
join    Table2 t2
on      t1.No = t2.No
        and t1.Date = t2.Date

答案 1 :(得分:0)

类似这样的事情

 SELECT T1.No, T1.Date, s_hour - L_hour AS result
 FROM T1 INNER JOIN
 T2 ON t1.No = t2.No AND 
 T1.Date = T2.Date

答案 2 :(得分:0)

SQL查询:

SELECT t1.no, t1.date1, t1.s_hour - t2.l_hour FROM t1, t2 
WHERE t1.no=t2.no AND t1.date1=t2.date1;

小提琴:Demo