2013-05-12 47 views
0

我試圖創建一個觸發器,使用人名和前三個字符的前兩個字符來生成唯一ID。 這是我目前正在通過錯誤:使用子串連接列的MySQL唯一用戶ID觸發器

#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 '@firstID varchar(2), DECLARE @lastID varchar (3), DECLARE @newID varchar (5); ' at line 3 

我也不認爲我可以通過只使用「+」,但CONCAT不能正常工作或者添加兩個(串)變量。請幫忙!

DELIMITER @@ 
CREATE TRIGGER EID BEFORE INSERT ON edrstaff FOR EACH ROW 
BEGIN 
DECLARE @firstID varchar(2), 
DECLARE @lastID varchar (3), 
DECLARE @newID varchar (5); 

SET @firstID = (
SELECT SUBSTRING(fname, FROM 1 FOR 2)) 
FROM edrstaff 
WHERE fname = new.fname) 

SET @lastID = (SELECT SUBSTRING(lname, FROM 1 FOR 3)) 

FROM edrstaff 
WHERE lname = new.lname); 

SET @newID = @firstID + @lastID; 

END @@ 

回答

0

嘗試

CREATE TRIGGER EID 
BEFORE INSERT ON edrstaff 
FOR EACH ROW 
    SET NEW.id = CONCAT(LEFT(NEW.fname, 2), LEFT(NEW.lname, 3)); 

SQLFiddle

+0

謝謝您的回覆!但我得到了這個錯誤#1054 - 'NEW'中的未知列'lname' – Louisa 2013-05-13 00:00:10

+0

不客氣。請發表表'edrstaff'結構(最好是CREATE TABLE語句的形式)和導致錯誤的INSERT語句。 – peterm 2013-05-13 03:03:22

+0

@Louisa你的問題需要更多幫助嗎? – peterm 2013-05-15 03:48:26