根据单元格值更新MySql表

时间:2012-11-27 12:30:26

标签: mysql

我在下面有一张各种专栏。 我想根据其他字段的值更新某些字段中的值。

我的表在这里

DROP TABLE IF EXISTS my_table; 
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT UNIQUE,
Shot VARCHAR(4),
lay VARCHAR(15) NOT NULL,
lay_avaibility VARCHAR(15) NOT NULL,
blk VARCHAR(10) NOT NULL,
blk_avaibility VARCHAR(15) NOT NULL,
pri VARCHAR(10) NOT NULL,
pri_avaibility VARCHAR(15) NOT NULL,
ani VARCHAR(10) NOT NULL,
ani_avaibility VARCHAR(15) NOT NULL,
status VARCHAR(5)
);

INSERT INTO my_table VALUES
(1,'SH01','1863','Offline','1863','Offline','P4645','Offline','P4557','Offline','Over'),
(2,'SH02','1863','Offline','P4645','Offline','P4557','Offline','1863','Offline','Over')
(3,'SH03','P4645','Offline','P4557','Offline','1863','Offline','1863','Offline','WIP'),
(4,'SH04','1863','Offline','P4645','Offline','P4557','Offline','1863','Offline','RTK'),
(5,'SH05','1863','Offline','1863','Offline','P4645','Offline','P4557','Offline','WIP'),
(6,'SH06','P4557','Offline','P4645','Offline','P4645','Offline','P4557','Offline','WIP');

SELECT * FROM my_table;

+----+------+-------+----------------+-------+----------------+-------+----------------+-------+----------------+--------+
| id | Shot | lay   | lay_avaibility | blk   | blk_avaibility | pri   | pri_avaibility | ani   | ani_avaibility | status |
+----+------+-------+----------------+-------+----------------+-------+----------------+-------+----------------+--------+
|  1 | SH01 | 1863  | Offline        | 1863  | Offline        | P4645 | Offline        | P4557 | Offline        | Over   |
|  2 | SH02 | 1863  | Offline        | P4645 | Offline        | P4557 | Offline        | 1863  | Offline        | Over   |
|  3 | SH03 | P4645 | Offline        | P4557 | Offline        | 1863  | Offline        | 1863  | Offline        | WIP    |
|  4 | SH04 | 1863  | Offline        | P4645 | Offline        | P4557 | Offline        | 1863  | Offline        | RTK    |
|  5 | SH05 | 1863  | Offline        | 1863  | Offline        | P4645 | Offline        | P4557 | Offline        | WIP    |
|  6 | SH06 | P4557 | Offline        | P4645 | Offline        | P4645 | Offline        | P4557 | Offline        | WIP    |
+----+------+-------+----------------+-------+----------------+-------+----------------+-------+----------------+--------+
6 rows in set (0.00 sec)

SELECT VERSION();

+-----------+
| VERSION() |
+-----------+
| 5.5.23    |
+-----------+
1 row in set (0.02 sec)

我想将所有lay_avaibility,blk_avaibility,pri_avaibility,ani_avaibility列更新为'在线',其中lay,pri,blk,ani列有'1863'即。,最终表格应如下所示

+----+------+-------+----------------+-------+----------------+-------+----------------+-------+----------------+--------+
| id | Shot | lay   | lay_avaibility | blk   | blk_avaibility | pri   | pri_avaibility | ani   | ani_avaibility | status |
+----+------+-------+----------------+-------+----------------+-------+----------------+-------+----------------+--------+
|  1 | SH01 | 1863  | Online         | 1863  | Online         | P4645 | Offline        | P4557 | Offline        | Over   |
|  2 | SH02 | 1863  | Online         | P4645 | Offline        | P4557 | Offline        | 1863  | Online         | Over   |
|  3 | SH03 | P4645 | Offline        | P4557 | Offline        | 1863  | Online         | 1863  | Online         | WIP    |
|  4 | SH04 | 1863  | Online         | P4645 | Offline        | P4557 | Offline        | 1863  | Online         | RTK    |
|  5 | SH05 | 1863  | Online         | 1863  | Online         | P4645 | Offline        | P4557 | Offline        | WIP    |
|  6 | SH06 | P4557 | Offline        | P4645 | Offline        | P4645 | Offline        | P4557 | Offline        | WIP    |
+----+------+-------+----------------+-------+----------------+-------+----------------+-------+----------------+--------+

3 个答案:

答案 0 :(得分:3)

您应该能够使用更新命令使用这样的CASE语句:

UPDATE my_table 
    SET lay_availability = CASE WHEN lay = '1863' THEN 'Online' ELSE lay_availability END,
    blk_availability = CASE WHEN blk = '1863' THEN 'Online' ELSE blk_availability END,
    pri_availability = CASE WHEN pri = '1863' THEN 'Online' ELSE pri_availability END,
    ani_avaibility   = CASE WHEN ani = '1863' THEN 'Online' ELSE ani_avaibility END;

答案 1 :(得分:0)

update my_table
set lay_avaibility = 'online',
blk_avaibility = 'online',
pri_avaibility = 'online',
ani_avaibility = 'online'
where (lay = 1863 or
blk = 1863 or 
ani =1863);

答案 2 :(得分:0)

试试这个

  UPDATE my_table 
SET lay_availability = CASE WHEN lay = '1863' THEN 'Online' ELSE 'Offline' END,
blk_availability = CASE WHEN blk = '1863' THEN 'Online' ELSE 'Offline' END,
pri_availability = CASE WHEN pri = '1863' THEN 'Online' ELSE 'Offline' END,
ani_avaibility   = CASE WHEN ani = '1863' THEN 'Online' ELSE 'Offline' END;