简体   繁体   中英

MYSQL - Use CONCAT to create a variable with contains an auto_increment value

After much research and testing, I still haven't managed to solve my problem.

I have a mysql table (user_ickle) which has 31 columns, one of which is the concatenation of two others, ie I concatenated the column "identifiantcorpora" with the column "id" to get the column "identifiantcorporabis".

I also created a trigger to update this new column.

CREATE TRIGGER insert_trigger
BEFORE INSERT ON User_ickle
FOR EACH ROW
SET new.identifiantcorporabis = CONCAT(identifiantcorpora,'',id)

But the problem is that when I want to insert something in my database, I get the following error message:

ERROR 1054 (42S22): Unknown column 'identifiantcorpora' in 'field list'. I insert like this INSERT INTO `user_ickle` VALUES (145,'Abkhazian','Afghanistan','Non-binary',45,'Adangme','Achinese','Achinese','Achinese','Achinese','Achinese','0','1','1','Austria - Wirtschafts- und Fremdsprachenakademie Salzburg','Online education has harmed the quality of higher education in Sri Lanka. ','Literary','No timing','No','Yes','lol',NULL,'0',1,'a61ba7fd-4df7-46f7-86f8-3a9ac0eb2b5c',2,3,2022,'12','learneridentifier','test')

So, looking in the forums, I saw that it could be a problem in the apostrophes but I don't see any. where I wrote "test", it corresponds to the value of identifiantcorpora.

I have created my identifiantcorpora column in this way: alter table user_ickle add column identifiantcorpora varchar(30).

Thank you for your help because I am desperate to find...

UPDATE :

I added new before identifiantcorpora and id: it works. However, my variable that normally autoincrements is no longer incrementing ... How can I do it? Because if I don't put a new before "id" then I have the same problem of unknown field.

The auto-increment value has not yet been generated at the time your BEFORE INSERT trigger runs. The value of new.id is NULL at this time.

The new.id column holds the auto-incremented value after the INSERT, so you could read it in an AFTER INSERT trigger. But then it's too late to change the value of new.identifiantcorporabis , because you can't change any column values in an AFTER INSERT trigger.

You can't do what you're trying to do in any kind of trigger. You can let the INSERT complete, and then subsequently run an UPDATE statement to concatenate the columns.


Re your comment:

Write no triggers. Forget about using a trigger for this task.

Insert a row, executing the insert from your client application.

The insert returns.

Update the same row, performing the concatenation that you wanted to do. Execute this from your client application.

UPDATE User_ickle 
SET identifiantcorporabis = CONCAT(identifiantcorpora,'',id) 
WHERE id = LAST_INSERT_ID();

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