我们可以在mysql 5.5中组合更新和选择查询吗?

时间:2015-11-27 15:45:32

标签: mysql select sql-update

我有一个这样的表,其中想要同时更新和选择。

CREATE TABLE ABCD_TABLE(ID VARCHAR(17) ,MAC VARCHAR(25),STATE SMALLINT,REGISTERED_MAC TEXT,TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

INSERT INTO ABCD_TABLE
    (`id`, `MAC`, `STATE`, `REGISTERED_MAC`, TIME)
VALUES
    (1, 'AB12CD65',"1",'NULL', NOW()),
    (2, 'BCSH9088',"2",'NULL', NOW()),
    (3, 'NHGTF5TT',"3",'NULL', NOW())
    ;

我们可以合并这些查询

update abcd_table
     set state = (case when state = 9 then 4 else state end),
         time = now()
     where id = "1";

     select REGISTERED_MAC FROM ABCD_TABLE WHERE STATE = "4" and id = "1";

0 个答案:

没有答案