0

在我的系统中,我想从数据库中获取员工的出勤数据,所以我写了一个巨大的 SQL 查询,它给了我相关的细节。但现在我需要特定结果的更新版本。所以我不知道如何将此查询放在更新语句中。

select * from(
select concat('pre:', date) as date,concat('pre:',employee_no) as employee_no,concat('pre:',name_with_initials) as name,concat('pre:',sign_in_at) as sign_in_at,concat('pre:',sign_out_at) as sign_out_at,emp from(               SELECT date, present.employee_no,employee_details.name_with_initials,present.sign_in_at, present.sign_out_at,employee_details.employee_no as emp  from ( 
                         SELECT employee_no,date,sign_in_at,sign_out_at FROM complete_shifts WHERE date = '2013-06-17'  UNION ALL
                         SELECT employee_no,date,sign_in_at,'00:00:00 ' AS sign_out_at  FROM incomplete_shifts WHERE date = '2013-06-17'  UNION ALL
                         SELECT employee_no,date,sign_in_at,'00:00:00 ' AS sign_out_at FROM incomplete_shift_records WHERE date = '2013-06-17' 
            )as present inner join employee_details on present.employee_no = employee_details.employee_no
) as final_present

   union all

select concat('ab:',date)as date,concat('ab:',employee_no)as employee_no,concat('ab:',name_with_initials)as name,concat('ab:',sign_in_at)as sign_in_at,concat('ab:',sign_out_at)as sign_out_at, emp from(
         select '2013-06-17' AS date,absent.employee_no,employee_details.name_with_initials,'00:00:00'as sign_in_at , '00:00:00' as sign_out_at,employee_details.employee_no as emp  from (                     
                   select * from ( SELECT employee_details.employee_no  FROM employee_details left outer join resigned_emps on 
                             employee_details.employee_no = resigned_emps.employee_no where resigned_emps.date is null or resigned_emps.date>'2013-06-17'
                  ) as available_emps  left outer join (
                  select employee_no from complete_shifts where date  = '2013-06-17'  union 
                  select employee_no from incomplete_shifts where date = '2013-06-17' union 
                  select employee_no from incomplete_shift_records where date  = '2013-06-17'
                  ) as present  on available_emps.employee_no = present.employee_no where present.employee_no is null 
        ) as absent inner join employee_details on absent.employee_no =  employee_details.employee_no
) as final_absent

)as final left outer join(  SELECT  leave.employee_no as lv_emp
                                                                                            FROM leave_dates  inner join leave on leave_dates.leave_id = leave.leave_id   where leave_dates.date  = '2013-06-17')as leave_emps 
on final.emp = leave_emps.lv_emp;
4

2 回答 2

0

对于如此大的查询,您应该将结果放在一个临时表中并从中进行更新。

create temporary table toupdate as
    <your query goes here>;

现在您可以调查将要更新的数据,以确保它确实没问题。

接下来,您可以将更新作为联接进行:

update table_to_update t join
       toupdate
       on t.key = toupdate.key
    set t.col = toupdate.col

因为您没有给出列或表的详细信息,所以这只是这种解决方案的结构。

于 2013-07-04T14:08:35.487 回答
0

而不是“select *”,而是使用适当的唯一字段来获取要更新的记录,例如 EmpID。最后将此结果用作内部查询结果来更新查询。

例子

Update ... set ... where empid in(您的选择查询在此处)

于 2013-07-04T13:35:32.840 回答