简体   繁体   中英

Adding two columns and storing in third column in mysql

I have tried several suggestions from stackoverflow, but I keep running into errors.

What I have is a table: test

I have 3 columns:

first (varchar)
last (varchar)
first_last (varchar)

What I want to do, is build a trigger, so when I enter first and last, and save it, the value of first_last is computed and saved. Here's what I have now:

Trigger name: update_first_last
Table: test
Time: AFTER
Event: INSERT
Definition: UPDATE test SET test.first_last=CONCAT(test.first, test.last)

The error I'm getting:

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

Both the insert statement and your trigger want to modify the whole table which is not allowed. You can make something like this instead:

CREATE TRIGGER update_first_last
BEFORE INSERT
ON test
FOR EACH ROW
SET NEW.first_last=CONCAT(NEW.first, NEW.last)

If the columns first and last are not defined as NOT NULL then you should create a similar trigger for an UPDATE action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM