mysql触发器,用于在多个int(0/1)上更新varchar depwning

vfh0ocws  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(290)

我有一张这样的table

+-------------+---------------------+------+-----+---------+----------------+
| 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    |                |
+-------------+---------------------+------+-----+---------+----------------+

我想更新一下 Kategori 每次 GR , WW 或者 FS 改变了。
如果 GR , WW 或者 FS1 值应为 GR, WW, FS 为了 kategori .
如果 GR=1 , WW=0 以及 FS=1 值应为 GR, FS 为了卡泰戈里等等。
我找不到有效的触发器。

4dc9hkyq

4dc9hkyq1#

如果您的版本支持它,您可以使用生成的列https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html 例如

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的排列

vojdkbi0

vojdkbi02#

下面的触发器应该可以完成这个任务。
它在更新表之前运行,如果没有为 kategory 字段,检查 GR , WW ,和 FS 为了设置它。
触发器识别一个事实 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 ;

相关问题