用于进/出时间考勤的SQL查询

时间:2018-05-11 13:57:39

标签: sql sql-server

我有一个带有以下示例输出的两个表。 tb1是及时的,tb2是时间 enter image description here

如果tb2中的行不在tb2中或tb1中的行不在tb1中,我想构建一个查询以使用空值来实现以下结果:

如果table2中的OINDX而不是table1中的IINDX或table1中的IINDX而不是table2中,则需要获取null

enter image description here

此代码创建两个表来尝试代码并帮助我

@zeit/next-css

3 个答案:

答案 0 :(得分:1)

试试这个...

SELECT *
FROM TIMEIN t1
FULL JOIN TIMEOUT t2 ON t1.IINDX = t2.OINDX

输出

+--------+--------+------------+----------------------+-----------+-------+--------+------------+----------------------+-----------+
| IINDX  | USERID |    Date    |      CHECKTIME       | CHECKTYPE | OINDX | USERID |    Date    |      CHECKTIME       | CHECKTYPE |
+--------+--------+------------+----------------------+-----------+-------+--------+------------+----------------------+-----------+
| 1      | 60     | 2018-02-07 | 2018-02-07T06:58:48Z | I         |     1 |     60 | 2018-02-07 | 2018-02-07T15:59:32Z | O         |
| 3      | 60     | 2018-02-08 | 2018-02-08T06:01:09Z | I         |     3 |     60 | 2018-02-08 | 2018-02-08T15:00:32Z | O         |
| 6      | 60     | 2018-02-09 | 2018-02-09T06:57:43Z | I         |     6 |     60 | 2018-02-09 | 2018-02-09T19:00:03Z | O         |
| 8      | 60     | 2018-02-10 | 2018-02-10T06:34:28Z | I         |     8 |     60 | 2018-02-10 | 2018-02-10T15:31:16Z | O         |
| 10     | 60     | 2018-02-11 | 2018-02-11T05:59:38Z | I         |    10 |     60 | 2018-02-11 | 2018-02-11T15:01:47Z | O         |
| 12     | 60     | 2018-02-12 | 2018-02-12T06:02:14Z | I         |    12 |     60 | 2018-02-12 | 2018-02-12T15:03:06Z | O         |
| 14     | 60     | 2018-02-13 | 2018-02-13T06:00:25Z | I         |    14 |     60 | 2018-02-13 | 2018-02-13T15:01:40Z | O         |
| 16     | 60     | 2018-02-14 | 2018-02-14T06:01:59Z | I         |    16 |     60 | 2018-02-14 | 2018-02-14T15:00:34Z | O         |
| 18     | 60     | 2018-02-15 | 2018-02-15T06:01:56Z | I         |    18 |     60 | 2018-02-15 | 2018-02-15T15:02:55Z | O         |
| 20     | 60     | 2018-02-22 | 2018-02-22T13:58:31Z | I         |    20 |     60 | 2018-02-22 | 2018-02-22T22:20:42Z | O         |
| 22     | 60     | 2018-02-23 | 2018-02-23T18:57:01Z | I         |    22 |     60 | 2018-02-24 | 2018-02-24T06:03:39Z | O         |
| 24     | 60     | 2018-02-24 | 2018-02-24T21:54:01Z | I         |    24 |     60 | 2018-02-25 | 2018-02-25T07:04:37Z | O         |
| 26     | 60     | 2018-02-25 | 2018-02-25T21:53:58Z | I         |    26 |     60 | 2018-02-26 | 2018-02-26T07:00:16Z | O         |
| 28     | 60     | 2018-02-26 | 2018-02-26T22:03:14Z | I         |    28 |     60 | 2018-02-27 | 2018-02-27T07:04:08Z | O         |
| 30     | 60     | 2018-02-27 | 2018-02-27T21:57:51Z | I         |    30 |     60 | 2018-02-27 | 2018-02-27T21:58:48Z | O         |
| (null) | (null) | (null)     | (null)               | (null)    |     4 |     60 | 2018-02-08 | 2018-02-08T15:00:34Z | O         |
| (null) | (null) | (null)     | (null)               | (null)    |    31 |     60 | 2018-02-28 | 2018-02-28T07:00:18Z | O         |
+--------+--------+------------+----------------------+-----------+-------+--------+------------+----------------------+-----------+

SQL FIDDLE:http://www.sqlfiddle.com/#!18/26468/20/0

答案 1 :(得分:0)

您需要使用"右外连接":

PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath setting

答案 2 :(得分:0)

我怀疑你真的想要full join

select i.*, o.*  -- note duplicate column names
from ins i full join
     outs o
     on i.iindx = o.iindx and i.id = o.id;

请注意,这样可以让您在任何一方都出现不匹配。