0

我有三张桌子。

  1. members它有一个可以为空的列,称为blocked,IDprimaryPhone
  2. blockedinfo它有 2 列 infotype.
  3. phoneNumbers它有ID, prefix, phoneNumber,memberID

当我在成员表中设置阻止字段时,我正在尝试在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.1TRIGGER members.before_updateBEFORE UPDATE ON membersFOR EACH ROW BEGIN 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(prefix ,phoneNumber`) AS 'phone' at line 4

4

2 回答 2

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'
于 2013-05-10T15:47:18.377 回答
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 ;
于 2013-05-10T15:08:28.987 回答