MySQL插入到带有CASE WHEN的选择查询中

时间:2019-01-31 05:25:24

标签: mysql insert case

我有一个INSERT INTO触发器,我希望SELECT部分​​评估一个字段的值(RDO),并根据RDO的“休息日”值与分配给该日期的DAYNAME相比较,将一个值返回给SchedStatus。家长时间表。时间表本身是在php文档的while循环中创建的(效果很好)。以下是我两次评估RDO值的尝试:

INSERT INTO sched_personnel (Sched_Shift_ID, UnitNumber, SupervisorNumber, Rank, Lunch, Assignment, Shift, Squad, RDOs, SchedStatus)  
      SELECT NEW.Sched_Shift_ID, UnitNumber, SupervisorNumber, Rank, Lunch, Assignment, Shift, Squad, RDO, @SchedStatus
      CASE
        WHEN RDO = 'SAT/SUN' AND (DAYNAME(NEW.ShiftDate) = 'Saturday' OR DAYNAME(NEW.ShiftDate) ='Sunday') THEN 'OFF'
        WHEN RDO = 'SUN/MON' AND (DAYNAME(NEW.ShiftDate) = 'Sunday' OR DAYNAME(NEW.ShiftDate) ='Monday') THEN 'OFF'
        WHEN RDO = 'MON/TUE' AND (DAYNAME(NEW.ShiftDate) = 'Monday' OR DAYNAME(NEW.ShiftDate) ='Tuesday') THEN 'OFF'
        WHEN RDO = 'TUE/WED' AND (DAYNAME(NEW.ShiftDate) = 'Tuesday' OR DAYNAME(NEW.ShiftDate) ='Wednesday') THEN 'OFF'
        WHEN RDO = 'WED/THU' AND (DAYNAME(NEW.ShiftDate) = 'Wednesday' OR DAYNAME(NEW.ShiftDate) ='Thursday') THEN 'OFF'
        WHEN RDO = 'THU/FRI' AND (DAYNAME(NEW.ShiftDate) = 'Thursday' OR DAYNAME(NEW.ShiftDate) ='Friday') THEN 'OFF'
        WHEN RDO = 'FRI/SAT' AND (DAYNAME(NEW.ShiftDate) = 'Friday' OR DAYNAME(NEW.ShiftDate) ='Saturday') THEN 'OFF'
        ELSE 'ONS'
      END AS @SchedStatus
      FROM accounts 
      WHERE accounts.Shift = NEW.Shift AND accounts.AccountStatus = 'Active';

我也尝试过:

INSERT INTO sched_personnel (Sched_Shift_ID, UnitNumber, SupervisorNumber, Rank, Lunch, Assignment, Shift, Squad, RDOs, SchedStatus)  
      SELECT NEW.Sched_Shift_ID, UnitNumber, SupervisorNumber, Rank, Lunch, Assignment, Shift, Squad, RDO, @SchedStatus
      CASE
        WHEN RDO = 'SAT/SUN' AND DAYNAME(NEW.ShiftDate) IN ('Saturday', 'Sunday') THEN 'OFF'
        WHEN RDO = 'SUN/MON' AND DAYNAME(NEW.ShiftDate) IN ('Sunday', 'Monday') THEN 'OFF'
        WHEN RDO = 'MON/TUE' AND DAYNAME(NEW.ShiftDate) IN ('Monday', 'Tuesday') THEN 'OFF'
        WHEN RDO = 'TUE/WED' AND DAYNAME(NEW.ShiftDate) IN ('Tuesday', 'Wednesday') THEN 'OFF'
        WHEN RDO = 'WED/THU' AND DAYNAME(NEW.ShiftDate) IN ('Wednesday', 'Thursday') THEN 'OFF'
        WHEN RDO = 'THU/FRI' AND DAYNAME(NEW.ShiftDate) IN ('Thursday', 'Friday') THEN 'OFF'
        WHEN RDO = 'FRI/SAT' AND DAYNAME(NEW.ShiftDate) IN ('Friday', 'Saturday') THEN 'OFF'
        ELSE 'ONS'
      END AS @SchedStatus
      FROM accounts 
      WHERE accounts.Shift = NEW.Shift AND accounts.AccountStatus = 'Active';

我收到一个错误:

SQL错误(1064):您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第4行的“ CASE WHEN RDO ='SAT / SUN'AND DAY NAME(NEW.Shift Date)IN('Saturday','Sunday')'附近使用。

如果消除查询的CASE部分,则将子记录添加到无误创建的每个父记录中。期望的最终状态是插入的每个子记录(sched_perssonel)都有一个“ SchedStatus”,该记录对应于个人的默认RDO,因此,CASE会比较该RDO与计划的日期(tabel sched_shift)进行评估。在正确的情况下我无法获取邮件。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我发现了3个错误

1)在您的代码中,有10列要插入,而11则在选择..

2)您的代码中有

  ...,   @SchedStatus

  CASE
    WHEN RDO = 'SAT/SUN' .......

您错过了@SchedStatus和CASE之间的逗号
    但是向您寻找代码..我认为您不需要@schedStatus并且我已删除

3)您在CASE末尾有一个别名@schedStatus ...但是使用insert select不需要别名

INSERT INTO sched_personnel (Sched_Shift_ID, UnitNumber, SupervisorNumber, Rank, Lunch, Assignment, Shift, Squad, RDOs, SchedStatus)  
    SELECT NEW.Sched_Shift_ID
      , UnitNumber
      , SupervisorNumber
      , Rank
      , Lunch
      , Assignment
      , Shift
      , Squad
      , RDO
      , CASE
      WHEN RDO = 'SAT/SUN' AND (DAYNAME(NEW.ShiftDate) = 'Saturday' OR DAYNAME(NEW.ShiftDate) ='Sunday') THEN 'OFF'
      WHEN RDO = 'SUN/MON' AND (DAYNAME(NEW.ShiftDate) = 'Sunday' OR DAYNAME(NEW.ShiftDate) ='Monday') THEN 'OFF'
      WHEN RDO = 'MON/TUE' AND (DAYNAME(NEW.ShiftDate) = 'Monday' OR DAYNAME(NEW.ShiftDate) ='Tuesday') THEN 'OFF'
      WHEN RDO = 'TUE/WED' AND (DAYNAME(NEW.ShiftDate) = 'Tuesday' OR DAYNAME(NEW.ShiftDate) ='Wednesday') THEN 'OFF'
      WHEN RDO = 'WED/THU' AND (DAYNAME(NEW.ShiftDate) = 'Wednesday' OR DAYNAME(NEW.ShiftDate) ='Thursday') THEN 'OFF'
      WHEN RDO = 'THU/FRI' AND (DAYNAME(NEW.ShiftDate) = 'Thursday' OR DAYNAME(NEW.ShiftDate) ='Friday') THEN 'OFF'
      WHEN RDO = 'FRI/SAT' AND (DAYNAME(NEW.ShiftDate) = 'Friday' OR DAYNAME(NEW.ShiftDate) ='Saturday') THEN 'OFF'
      ELSE 'ONS'
    END A
    FROM accounts 
    WHERE accounts.Shift = NEW.Shift AND accounts.AccountStatus = 'Active';