[英]MySQL SELECT inside VALUE + max(ID)+1
Any idea how can I change max(ID)+1 to select the highest auto increment value from wp_post table. 任何想法如何更改max(ID)+1来从wp_post表中选择最高的自动增量值。 I want to make sure I can choose equivalent of max(ID)+1 我想确保我可以选择max(ID)+1的等效项
INSERT INTO `wp_posts`(`ID`, `post_author`, `post_title`, `post_status`, `comment_status`,`post_type`)
VALUES ((SELECT max(ID)+1 FROM wp_posts),1,'name lastname', 'publish', 'closed', 'post_type');
Above query has an error, cos it's not possible to select max+1 from the same table where an INSERT statement is set up. 上面的查询有错误,因为不可能从设置INSERT语句的同一表中选择max + 1。
You suggest that id
is auto-incremented. 您建议id
自动增加。 If not, then it should be. 如果不是,那么应该是。 Then you would write the statement as: 然后,您将语句写为:
INSERT INTO `wp_posts`(`post_author`, `post_title`, `post_status`,
`comment_status`,`post_type`)
VALUES (1, 'name lastname', 'publish', 'closed', 'post_type');
Like Gordon Linoff says use ID as auto-increment here is create table example.. 就像戈登·利诺夫 ( Gordon Linoff)所说,使用ID作为自动增量,这是创建表的示例。
CREATE TABLE wp_posts
(
ID int NOT NULL AUTO_INCREMENT,
post_author varchar(255) NOT NULL,
post_title varchar(255),
post_status varchar(255),
comment_status varchar(255),
post_type varchar(255),
PRIMARY KEY (ID)
)
Than you can insert as Gordon mentioned.. 比您可以像戈登提到的那样插入。
INSERT INTO `wp_posts`(`post_author`, `post_title`, `post_status`,
`comment_status`,`post_type`)
VALUES (1, 'name lastname', 'publish', 'closed', 'post_type');
Use auto-increment . 使用自动增量 。
The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows. AUTO_INCREMENT属性可用于为新行生成唯一标识。
You can add an auto increment to an existing table to achieve what you are trying to do. 您可以向现有表添加自动增量以实现您要执行的操作。 ALTER TABLE wp_posts MODIFY id int(11) AUTO_INCREMENT;
set @ID=(SELECT if(max(id) is null,1, max(id)+1) FROM wp_posts); 设置@ ID =(选择if(max(id)为null,1,max(id)+1)FROM wp_posts);
INSERT INTO wp_posts
( ID
, post_author
, post_title
, post_status
, comment_status
, post_type
) VALUES (@ID,1,'name lastname', 'publish', 'closed', 'post_type'); 插入wp_posts
( ID
, post_author
, post_title
, post_status
, comment_status
, post_type
)值(@ ID,1,'姓氏','发布','关闭','post_type');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.