简体   繁体   English

SQL插入字段,具有其他自动递增字段的值+值

[英]SQL insert field, with value of other auto increment field + value

It might sound complicated, but it's not. 听起来可能很复杂,但事实并非如此。 I have a table called "orders", with fields: 我有一个名为“ orders”的表,其中包含字段:

id INT(11) auto_increment,
realid INT(14)

Now, I want at every insert into this table, do something like: 现在,我想在此表的每个插入处执行以下操作:

INSERT INTO orders VALUES (null, id+1000);

However I'll be doing it on shop which is currently online, and I want to change everything in 5 minutes. 但是,我将在当前在线的商店中进行购买,我希望在5分钟内更改所有内容。 Will something like this work? 这样的东西行吗? If not, how do I do that? 如果没有,我该怎么办?

I would think a simpler solution would be a calculated column presuming your DBMS supports it (This is using SQL Server syntax, although the MySql syntax should be nearly identical except that you would use AUTO_INCREMENT instead of Identity(1,1) ): 我认为一个更简单的解决方案是假设您的DBMS支持它的计算列(这是使用SQL Server语法,尽管MySql语法应该几乎相同,除了您将使用AUTO_INCREMENT而不是Identity(1,1) ):

Create Table Foo    (
                    Id int not null identity(1,1)
                    , Name varchar(50)
                    , Bar As Id + 1000 
                    )

Of course, you could also just do it in the presentation or business tier instead of the database. 当然,您也可以只在表示层或业务层而不是数据库中进行操作。

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

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