Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用子查询和数据-MySQL_Php_Mysql - Fatal编程技术网

Php 使用子查询和数据-MySQL

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

在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, 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()

您尝试过吗?发生了什么事?提示:调用函数时无需
选择
“未发生任何事”显示错误。