在插入mysqli时,是否可以使用子查询并同时在值中使用数据?使用子查询和数据 - MySQL
insert into articles(description) values('Information');
insert into blogs(blog, article_id) values('example.com/', SELECT LAST_INSERT_ID());
在插入mysqli时,是否可以使用子查询并同时在值中使用数据?使用子查询和数据 - MySQL
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)
但作为@Pekka建议在评论上述,在这种情况下,你甚至不需要一个子查询。这将工作:
INSERT INTO blogs (blog, article_id) VALUES ('example.com/', LAST_INSERT_ID());
非常感谢。 – user453984 2010-10-11 13:35:03
您可能想要使用insert...select
来代替。
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
所以这会是这样的
insert into blogs(...) select 'example.com/', last_insert_id()
你试过了吗?发生了什么? – 2010-10-11 11:02:41
提示:调用某个函数时不需要'SELECT' – 2010-10-11 11:03:28
没有任何事情显示错误。 – user453984 2010-10-11 13:29:19