Sinan
Sinan

Reputation: 5980

mysql update another column to primary key value

Is there a way to set the value of another column to primary key (auto increment)?

Basically what I am trying to achieve is this

ID     Stuff
----   ------
1      1
2      324
3      64
4      94
5      ...

Now when I am adding the the fifth row with a query like

INSERT into TABLE values(NULL, NULL);

So when the second value is NULL I want it to be equal to ID.

I tried INSERT triggers but it doesnt work. Any ideas?

Upvotes: 2

Views: 1232

Answers (3)

Timo Huovinen
Timo Huovinen

Reputation: 55643

Try this:

INSERT INTO `TABLE` (`ID`,`Stuff`) VALUES(NULL, `ID`);

Upvotes: 0

dugas
dugas

Reputation: 12463

One possibility is to expose a stored procedure, and when the Stuff parameter is null, update the insert with the LAST_INSERT_ID(), otherwise pass the non-null value to the insert.

Upvotes: 0

Z .
Z .

Reputation: 12837

I don't think you can do that in one step, but you can first insert and then update...

Upvotes: 1

Related Questions