Insert ID into another field of the same table at the time of INSERT

Asked

Viewed 122 times

-3

I’m in the following situation, I’ve been searching the net and found similar aids but not what I really need to know and I don’t even know if it’s possible!

Come on! There would be some way for me to put the ID number that will be inserted in the INSERT query and clone it to another field of the same table but, all of this in the same query?


table:

users

fields:

id (Auto increment)

id_member


I would like to do a query where the number that will be generated in the id field is also placed in the id_member field.

Thanks!

  • Okay, edited out....

  • Then beseech the INSERT, shall not effect a UPDATE?

  • Yes it was what I would do, but I wanted to know if it was possible to run everything in the same query, thinking of optimizing the code!

1 answer

1


Create a TRIGGER AFTER INSERT to accomplish a UPDATE:

CREATE TRIGGER new_loaner_added
AFTER INSERT ON usuarios
FOR EACH ROW
  UPDATE usuarios
     SET id_membro = NEW.id
   WHERE id = NEW.id;

TRIGGER

A Trigger is a named database Object that is Associated with a table, and that activates when a particular Event occurs for the table.

In free translation:

A Trigger is a database named object that is associated with a table, and is activated when a particular event occurs for the table.

  • OK I will try to adapt in my code and return! I did not know it was possible to run an UPDATE in the same query

  • How would you adapt in a common INSERT? because I have more values to be entered the examples I gave above with only 1 field were hypothetical, An example of what I have : "INSERT INTO users (pca_member,pca_email,pca_name) VALUES ('{$id_rescued auto_increment}','{$email}','{$name}')";

  • Quiet but anyway the information was useful, I will choose to make a later update even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.