我有三张桌子。
members
并且它有一个名为blocked
,ID
和primaryPhone
的可空列。blockedinfo
它有2列info
和type
。phoneNumbers
它有ID
,prefix
,phoneNumber
,memberID
我正在尝试在members
表上创建一个触发器,以便在我在members表中设置阻塞字段时将用户主要电话号码插入blockedinfo
表。当我将blockedinfo
字段设置为blocked
NULL
中删除
CREATE TRIGGER `members.before_update` BEFORE UPDATE ON `members` FOR EACH ROW BEGIN
IF( NEW.`blocked` IS NOT NULL ) THEN
INSERT INTO `blockedinfo` ( phone, 'userPhoneNumberValue')
SELECT CONCAT( `prefix` , `phoneNumber` ) AS 'phone'
FROM `phonenumbers`
WHERE `ID` = NEW.`primaryPhone`
AND `memberID` = NEW.`ID`
LIMIT 1 ;
ELSE
DELETE FROM `blockedinfo` WHERE `info` IN (
SELECT CONCAT( `prefix` , `phoneNumber` )
FROM `phonenumbers`
WHERE `ID` = NEW.`primaryPhone`
AND `memberID` = NEW.`ID`);
END IF ;
END ;
尝试构建触发器时出现此错误
以下查询失败:“CREATE DEFINER = root
@ 127.0.0.1
TRIGGER members.before_update
在更新members
之前,每行开始MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''phoneNumberValue') SELECT CONCAT(
前缀{{ 1}} phoneNumber`)第4行的“电话”
答案 0 :(得分:1)
首先,在这部分
INSERT INTO `blockedinfo` ( phone, 'userPhoneNumberValue')
SELECT CONCAT( `prefix` , `phoneNumber` ) AS 'phone'
FROM `phonenumbers`
您提供了一个字符串文字“userPhoneNumberValue”,其中应在“blockedinfo”中提供列名。为此处定位的目标命名。
INSERT INTO `blockedinfo` (info, type)
在这部分
SELECT CONCAT( `prefix` , `phoneNumber` ) AS 'phone'
你提供一个价值。你需要提供两个。我猜你需要更像这样的东西。
SELECT CONCAT(`prefix`, `phoneNumber`) AS phone, 'userPhoneNumberValue'
答案 1 :(得分:0)
INSERT INTO `blockedinfo` ( phone, `phoneNumber`)
SELECT CONCAT( `prefix` , `phoneNumber` ) AS `phone`
在这里,您尝试插入2个值,但您的选择仅返回1
尝试:
INSERT INTO `blockedinfo` ( phone, `phoneNumber`)
SELECT CONCAT( `prefix` , `phoneNumber` ) AS `phone`, `phoneNumber`
FROM `phonenumbers`
WHERE `ID` = NEW.`primaryPhone`
AND `memberID` = NEW.`ID`
LIMIT 1 ;