简体   繁体   English

mysql将另一列更新为主键值

[英]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. 因此,当第二个值是NULL我希望它等于ID。

I tried INSERT triggers but it doesnt work. 我尝试了INSERT触发器,但是它不起作用。 Any ideas? 有任何想法吗?

我不认为您可以一步一步完成,但是您可以先插入然后更新...

一种可能是公开存储过程,并且当Stuff参数为null时,使用LAST_INSERT_ID()更新插入,否则将非null值传递给插入。

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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