sql insert into,select,where语句

时间:2016-11-17 13:35:09

标签: mysql sql

我想将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

2 个答案:

答案 0 :(得分:0)

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 IN ( '-', 'Long Stay' , 'Check Out')
 ;

这应该有帮助

答案 1 :(得分:0)

在第一个查询中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表中的所有行复制到除row之外的hk_history中   使用rStatus =&#39; - &#39;或者rStatus =&#39;长期住宿&#39;和rStatus =&#39;退房&#39;。

只需使用一个声明:

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')

但是,之前的版本似乎更清楚你的意图。