我想将hk_room表中的所有行复制到hk_history中,除了带有rStatus ='-'或rStatus ='Long Stay'和rStatus ='Check Out'的行。

“-”是rStatus属性的默认值。

我试过这两个查询:

INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1;
WHERE rStatus <> '-';


要么

INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1;
WHERE rStatus = 'Long Stay' AND rStatus = 'Check Out';


but I got this error

最佳答案

在第一个查询中,您在where之前有一个分号:

INSERT INTO hk_history
    (rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
    SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
    FROM hk_room1;
    -------------^
    WHERE rStatus <> '-';


您需要删除它。

为达到这个:


  想要将hk_room表中的所有行复制到hk_history中,但行除外
  rStatus ='-'或rStatus ='Long Stay'和rStatus ='Check Out'。


只需使用一条语句:

INSERT INTO hk_history
    (rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
    SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
    FROM hk_room1
    WHERE NOT (rStatus = '-' OR
               rStatus = 'Long Stay' and rStatus = 'Check Out'
              );


您也可以将其表达为:

    WHERE rStatus <> '-' AND
          (rStatus <> 'Long Stay' OR rStatus <> 'Check Out')


但是,以前的版本似乎更符合您的意图。

关于mysql - sql插入,选择,where语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40656340/

10-10 10:48