mysql触发器:插入后,根据输入的内容+1?
mysql trigger: after insert, +1 depending on what is entered?
CREATE TABLE `inventory` (
`id` int(11) NOT NULL,
`owner` int(11) DEFAULT NULL,
`grade1` int(11) DEFAULT NULL,
`grade2` int(11) NOT NULL,
`grade3` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `inventory`
--
INSERT INTO `inventory` (`id`, `owner`, `grade1`, `grade2`, `grade3`) VALUES
(3, 1, 2, 1, 1);
-- --------------------------------------------------------
--
-- Table structure for table `transfer`
--
CREATE TABLE `transfer` (
`id` int(11) NOT NULL,
`owner` int(11) DEFAULT NULL,
`total` char(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transfer`
--
INSERT INTO `transfer` (`id`, `owner`, `total`) VALUES
(20, 1, 1);
--
-- Triggers `transfer`
--
DELIMITER $$
CREATE TRIGGER `t` AFTER INSERT ON `transfer` FOR EACH ROW update inventory t1
set t1.grade1 = t1.grade1 + 1
WHERE t1.owner = new.owner
AND `total` = '/1'
$$
DELIMITER ;
我有两个表,你可以从上面的代码中看到。我正在 MySQL 中使用触发器。
我想做的是,当有人将某物输入转账时,所有者与库存中的所有者相匹配 - 如果他们在转账中输入“(数字)/1” ,它会给 grade1 加 1。如果他们在总计中输入“(一个数字)/2”,它会将 1 添加到 grade2。三年级也一样。从上面的触发器可以看出,这是我尝试过的。我在没有 AND `total` = '/1'
的情况下尝试过,所以我知道问题一定出在那个部分。我也尝试过不使用 ` around total,但是没有它它无法识别此列。
我查看了 SO,但找不到任何解决方法。
我需要通过触发器完成此部分 - 如果有人有任何想法,请告诉我。谢谢
有条件地更新不同列的通常方法是更新所有列,但是使用条件来确定是给它们新值还是保留旧值。这可以像任何其他 UPDATE
查询一样用在触发器中。
CREATE TRIGGER `t` AFTER INSERT ON `transfer` FOR EACH ROW update inventory t1
set t1.grade1 = IF(new.total LIKE '%/1', t1.grade1 + 1, t1.grade1),
t1.grade2 = IF(new.total LIKE '%/2', t1.grade2 + 1, t1.grade2),
t1.grade3 = IF(new.total LIKE '%/3', t1.grade3 + 1, t1.grade3)
WHERE t1.owner = new.owner
看来您真正需要的是 transfer
table 中的第四列 grade
,这样您的触发代码就可以知道要增加哪个等级。触发器中只有 OLD.*
和 NEW.*
,即发生更改的行的列。您不能让当前的整数列之一携带比简单整数更多的额外信息。
ALTER TABLE transfer ADD COLUMN grade TINYINT UNSIGNED;
然后您可以在触发器中使用它来判断要增加哪个等级。
CREATE TRIGGER `t` AFTER INSERT ON `transfer` FOR EACH ROW
UPDATE inventory
SET grade1 = grade1 + CASE NEW.grade WHEN 1 THEN 1 ELSE 0 END,
grade2 = grade2 + CASE NEW.grade WHEN 2 THEN 1 ELSE 0 END,
grade3 = grade3 + CASE NEW.grade WHEN 3 THEN 1 ELSE 0 END;
WHERE owner = NEW.owner
CREATE TABLE `inventory` (
`id` int(11) NOT NULL,
`owner` int(11) DEFAULT NULL,
`grade1` int(11) DEFAULT NULL,
`grade2` int(11) NOT NULL,
`grade3` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `inventory`
--
INSERT INTO `inventory` (`id`, `owner`, `grade1`, `grade2`, `grade3`) VALUES
(3, 1, 2, 1, 1);
-- --------------------------------------------------------
--
-- Table structure for table `transfer`
--
CREATE TABLE `transfer` (
`id` int(11) NOT NULL,
`owner` int(11) DEFAULT NULL,
`total` char(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transfer`
--
INSERT INTO `transfer` (`id`, `owner`, `total`) VALUES
(20, 1, 1);
--
-- Triggers `transfer`
--
DELIMITER $$
CREATE TRIGGER `t` AFTER INSERT ON `transfer` FOR EACH ROW update inventory t1
set t1.grade1 = t1.grade1 + 1
WHERE t1.owner = new.owner
AND `total` = '/1'
$$
DELIMITER ;
我有两个表,你可以从上面的代码中看到。我正在 MySQL 中使用触发器。
我想做的是,当有人将某物输入转账时,所有者与库存中的所有者相匹配 - 如果他们在转账中输入“(数字)/1” ,它会给 grade1 加 1。如果他们在总计中输入“(一个数字)/2”,它会将 1 添加到 grade2。三年级也一样。从上面的触发器可以看出,这是我尝试过的。我在没有 AND `total` = '/1'
的情况下尝试过,所以我知道问题一定出在那个部分。我也尝试过不使用 ` around total,但是没有它它无法识别此列。
我查看了 SO,但找不到任何解决方法。
我需要通过触发器完成此部分 - 如果有人有任何想法,请告诉我。谢谢
有条件地更新不同列的通常方法是更新所有列,但是使用条件来确定是给它们新值还是保留旧值。这可以像任何其他 UPDATE
查询一样用在触发器中。
CREATE TRIGGER `t` AFTER INSERT ON `transfer` FOR EACH ROW update inventory t1
set t1.grade1 = IF(new.total LIKE '%/1', t1.grade1 + 1, t1.grade1),
t1.grade2 = IF(new.total LIKE '%/2', t1.grade2 + 1, t1.grade2),
t1.grade3 = IF(new.total LIKE '%/3', t1.grade3 + 1, t1.grade3)
WHERE t1.owner = new.owner
看来您真正需要的是 transfer
table 中的第四列 grade
,这样您的触发代码就可以知道要增加哪个等级。触发器中只有 OLD.*
和 NEW.*
,即发生更改的行的列。您不能让当前的整数列之一携带比简单整数更多的额外信息。
ALTER TABLE transfer ADD COLUMN grade TINYINT UNSIGNED;
然后您可以在触发器中使用它来判断要增加哪个等级。
CREATE TRIGGER `t` AFTER INSERT ON `transfer` FOR EACH ROW
UPDATE inventory
SET grade1 = grade1 + CASE NEW.grade WHEN 1 THEN 1 ELSE 0 END,
grade2 = grade2 + CASE NEW.grade WHEN 2 THEN 1 ELSE 0 END,
grade3 = grade3 + CASE NEW.grade WHEN 3 THEN 1 ELSE 0 END;
WHERE owner = NEW.owner