Php 使用子查询和数据-MySQL
在mysqli中插入时,是否可以使用子查询并同时使用值中的数据Php 使用子查询和数据-MySQL,php,mysql,Php,Mysql,在mysqli中插入时,是否可以使用子查询并同时使用值中的数据 insert into articles(description) values('Information'); insert into blogs(blog, article_id) values('example.com/', SELECT LAST_INSERT_ID()); 是的,你可以。只需将子查询括在括号中即可。例如: CREATE TABLE articles (id int not null primary key
insert into articles(description) values('Information');
insert into blogs(blog, article_id) values('example.com/', SELECT LAST_INSERT_ID());
是的,你可以。只需将子查询括在括号中即可。例如:
CREATE TABLE articles (id int not null primary key, description varchar(50));
Query OK, 0 rows affected (0.06 sec)
CREATE TABLE blogs (id int not null primary key, blog varchar(50), article_id int);
Query OK, 0 rows affected (0.06 sec)
INSERT INTO articles (description) VALUES ('Information');
Query OK, 1 row affected, 1 warning (0.04 sec)
INSERT INTO blogs (blog, article_id) VALUES ('example.com/', (SELECT LAST_INSERT_ID()));
Query OK, 1 row affected, 1 warning (0.04 sec)
但正如上面的评论所建议的,在本例中,您甚至不需要子查询。这本可以奏效的:
INSERT INTO blogs (blog, article_id) VALUES ('example.com/', LAST_INSERT_ID());
您可能需要使用
insert…选择
所以会是这样的
insert into blogs(…)选择'example.com/',last\u insert\u id()
您尝试过吗?发生了什么事?提示:调用函数时无需选择
“未发生任何事”显示错误。