2013-09-28 78 views
0

所以,我有一個表,通過以下插入觸發更新某些列

CREATE TABLE IF NOT EXISTS `employees` (
`e_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
`e_username` varchar(100) NOT NULL, 
`left_id` int(10) unsigned NOT NULL, 
`right_id` int(10) unsigned NOT NULL, 
`e_ssn` int(5) unsigned NOT NULL DEFAULT '0', 
`e_first_name` varchar(100) NOT NULL, 
`e_last_name` varchar(100) NOT NULL, 
`e_address` text NOT NULL, 
`e_location` int(10) unsigned DEFAULT NULL, 
UNIQUE KEY `e_id` (`e_id`), 
UNIQUE KEY `e_username` (`e_username`) 
) 

給出的SQL描述它具有給定的數據

INSERT INTO `employees` (`e_id`, `e_username`, `left_id`, `right_id`, `e_ssn`, `e_first_name`, `e_last_name`, `e_address`, `e_location`) VALUES 
(1, '100884', 1, 8, 444444444, 'James', 'Burnbridge', '1123 fdkjfjdkfjdfkjd', 76), 
(2, '100885', 2, 7, 0, 'Frank', 'Grimsley', '123 Fayke St', 76), 
(5, '100886', 3, 4, 0, 'Mark', 'Hill', 'kfjdkfjdf\r\nkofjdkfjdkfdj', 76), 
(6, '100887', 5, 6, 0, 'fdfdfd', 'fdfdfd', 'fdfdfdf', 76); 

我想創建一個觸發器,它使所以,當我輸入一個新的行,像這樣

INSERT INTO employees(e_username,left_id,right_id,e_ssn, e_first_name, e_last_name, e_address, e_location) 
VALUES('100888', 0, 0, 0, 'New','User', '100 NotReal st city, state zip', 76) 

我想爲left_id和right_id列要更新基礎上,在他們的位置有它的right_id第一用戶的right_id不同於它是由1

這裏left_id是我到目前爲止,但它給了我一個錯誤

DROP TRIGGER IF EXISTS `after_insert_employees` ; 

CREATE DEFINER = `root`@`localhost` TRIGGER `after_insert_employees` AFTER INSERT ON `employees` 
FOR EACH 
ROW BEGIN 
DECLARE r_id INT; 
SELECT max(right_id) INTO r_id FROM employees WHERE e_location=new.e_location AND right_id = left_id+1; 
UPDATE employees SET left_id = r_id+1 AND right_id = r_id+2 WHERE e_id = new.e_id; 
END 

我得到這個錯誤,當我試圖插入東西

#1442 - Can't update table 'employees' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

我迷失在這一個。任何幫助?

+0

你嘗試之前插入扳機? – jomsk1e

回答