更新从另一个表获取的表数据

时间:2019-10-07 05:23:47

标签: sql sql-server tsql sql-update

我有一个表,该表存储有关一名员工的出勤信息,而另一张表中存储有关该雇员的轮班信息,这基本上是值班表。

这是考勤表的结构

CREATE TABLE Attendance
(
ID INT,
EmpCode INT,
ShiftCode INT, 
CheckIn DATETIME,
CheckOut DATETIME
)



INSERT INTO Attendance VALUES (1, 1, 1, '2019-09-01 09:16:23', NULL)
INSERT INTO Attendance VALUES (2, 1, 1, NULL, '2019-09-01 18:01:56')
INSERT INTO Attendance VALUES (3, 1, 2, '2019-09-02 09:00:00', NULL)
INSERT INTO Attendance VALUES (4, 1, 2, NULL, '2019-09-02 18:48:21')
INSERT INTO Attendance VALUES (5, 1, 1, '2019-09-13 09:27:00', NULL)
INSERT INTO Attendance VALUES (6, 1, 1, NULL, '2019-09-13 18:45:00')
INSERT INTO Attendance VALUES (7, 2, 2, '2019-09-01 21:19:17', NULL)
INSERT INTO Attendance VALUES (8, 2, 2, NULL, '2019-09-01 23:30:56')
INSERT INTO Attendance VALUES (9, 2, 2, '2019-09-05 09:23:00', NULL)
INSERT INTO Attendance VALUES (10, 2, 2, NULL, '2019-09-05 17:19:00')

这是值班名册的结构和样本数据。

CREATE TABLE Shifts
(
ID INT PRIMARY KEY,
EmpCode INT,
ShiftCode INT,
StartDate DATETIME,
EndDate DATETIME
)

INSERT INTO Shifts VALUES (1, 1, 24, '2019-09-01 00:00:00', '2019-09-05 00:00:00');
INSERT INTO Shifts VALUES (2, 2, 25, '2019-09-01 00:00:00', '2019-09-05 00:00:00');

此想法是将出勤表wrt中的ShiftCode更新为值班名册中存储的班次。因此,如果员工1的出勤介于'2019-09-01''2019-09-05'之间,则该员工的轮班代码应更新为24,其他员工的轮班代码也应更新为<?php $ql_cart_wrap_color = (WC()->cart->get_cart_contents_count() == 0 ) ? 'ql_cart_wrap_color_gray' : 'ql_cart_wrap_color_orange';?> <div class="ql_cart_wrap woo_amc_open_active <?php echo $ql_cart_wrap_color; ?>"> <button href="javascript:" class="ql_cart-btn"> <span class="count"><?php echo esc_html( WC()->cart->cart_contents_count );?></span> <span class="cart-header"></span> </button> </div> 。如果对于出勤表中的日期不存在值班名册,则不应对其进行更新并按原样进行。 我需要更新查询。

3 个答案:

答案 0 :(得分:1)

类似这样的东西:

SELECT *
FROM Attendance A
INNER JOIN Shifts S
    ON A.EmpCode = S.[EmpCode]
    AND 
    (
        A.CheckIn BETWEEN S.[StartDate] AND S.[EndDate]
        OR
        A.CheckOut BETWEEN S.[StartDate] AND S.[EndDate]
    )

并进行了更新:

UPDATE Attendance
SET ShiftCode = S.[ShiftCode]
FROM Attendance A
INNER JOIN Shifts S
    ON A.EmpCode = S.[EmpCode]
    AND 
    (
        A.CheckIn BETWEEN S.[StartDate] AND S.[EndDate]
        OR
        A.CheckOut BETWEEN S.[StartDate] AND S.[EndDate]
    );

答案 1 :(得分:0)

我已经尝试过了,它也可以工作:

int main() {
    int var = 0;
    return [] { return sizeof(var); }(); // var is not captured!
}

答案 2 :(得分:0)

尝试一下。将会有帮助

Actual result
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'