MySQL触发器可在许多int(0/1)上更新varchar depwnding

时间:2018-12-27 15:11:19

标签: mysql sql

我有这样的表

+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| _id         | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| name        | varchar(100)        | NO   |     | NULL    |                |
| kategori    | varchar(50)         | NO   |     | NULL    |                |
| GR          | int(1)              | NO   |     | NULL    |                |
| WW          | int(1)              | NO   |     | NULL    |                |
| FS          | int(1)              | NO   |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+

我想每次更改KategoriGRWW时更新FS

如果GRWWFS1,则GR, WW, FS的值应为kategori

如果GR=1WW=0FS=1的值,则对于kategori等应为GR, FS

我找不到能起作用的触发器。

2 个答案:

答案 0 :(得分:1)

下面的触发器应该可以解决问题。

它在表上更新前运行,并且如果没有为kategory字段提供值,则检查GRWWFS进行设置。

触发器认识到UPDATE可能会在未设置所有3个字段的情况下发生:在这种情况下,它查找数据库中该字段的现有值。在这种情况下,这应该允许kategori与数据保持同步。

delimiter $$
CREATE TRIGGER updateMyTable
BEFORE UPDATE ON my_table
FOR EACH ROW 
BEGIN
    IF NEW.kategori IS NULL THEN
        IF COALESCE(NEW.GR, OLD.GR) = 1 THEN 
            SET NEW.kategori = IF( NEW.kategori IS NULL, 'GR', CONCAT(NEW.kategori, ',', 'GR') );
        END IF;
        IF COALESCE(NEW.WW, OLD.WW) = 1 THEN 
            SET NEW.kategori = IF( NEW.kategori IS NULL, 'WW', CONCAT(NEW.kategori, ',', 'WW') );
        END IF;
        IF COALESCE(NEW.FS, OLD.FS) = 1 THEN 
            SET NEW.kategori = IF( NEW.kategori IS NULL, 'FS', CONCAT(NEW.kategori, ',', 'FS') );
        END IF;
    END IF;
END$$
delimiter ;

答案 1 :(得分:0)

如果您的版本支持,则可以使用例如生成的列documentation

DROP TABLE IF EXISTS T;
create table t(id int auto_increment primary key,gw int,ww int, fs int
,category varchar(100) as (
case 
        when gw = 1 and ww = 1 and fs = 1 then 'gw,ww,fs'
        when gw = 1 and ww = 1 then 'gw,ww'
        when gw = 1 and fs = 1 then 'gw,fs'
        when gw = 1 and fs = 1 then 'gw,fs'
        when ww = 1 and fs = 1 then 'ww,fs'
        when gw = 1 then 'gw'
        when ww = 1 then 'ww'
        when fs = 1 then 'fs'
        else null
        end 
        )
);

insert into t (gw,ww,fs) values 
(1,1,1),
(1,1,0),
(1,0,0),
(0,1,1),
(0,0,1),
(1,0,1);

select *,
        case 
        when gw = 1 and ww = 1 and fs = 1 then 'gw,ww,fs'
        when gw = 1 and ww = 1 then 'gw,ww'
        when gw = 1 and fs = 1 then 'gw,fs'
        when gw = 1 and fs = 1 then 'gw,fs'
        when ww = 1 and fs = 1 then 'ww,fs'
        when gw = 1 then 'gw'
        when ww = 1 then 'ww'
        when fs = 1 then 'fs'
        else null
        end  cat
from t;

+----+------+------+------+----------+----------+
| id | gw   | ww   | fs   | category | cat      |
+----+------+------+------+----------+----------+
|  1 |    1 |    1 |    1 | gw,ww,fs | gw,ww,fs |
|  2 |    1 |    1 |    0 | gw,ww    | gw,ww    |
|  3 |    1 |    0 |    0 | gw       | gw       |
|  4 |    0 |    1 |    1 | ww,fs    | ww,fs    |
|  5 |    0 |    0 |    1 | fs       | fs       |
|  6 |    1 |    0 |    1 | gw,fs    | gw,fs    |
+----+------+------+------+----------+----------+
6 rows in set (0.00 sec)

update t set gw = 0 where id = 1;

+----+------+------+------+----------+-------+
| id | gw   | ww   | fs   | category | cat   |
+----+------+------+------+----------+-------+
|  1 |    0 |    1 |    1 | ww,fs    | ww,fs |
|  2 |    1 |    1 |    0 | gw,ww    | gw,ww |
|  3 |    1 |    0 |    0 | gw       | gw    |
|  4 |    0 |    1 |    1 | ww,fs    | ww,fs |
|  5 |    0 |    0 |    1 | fs       | fs    |
|  6 |    1 |    0 |    1 | gw,fs    | gw,fs |
+----+------+------+------+----------+-------+

我让你算出gw,ww,fs的排列