我不知道为什么不能将 INSERT INTO 与 WITH 语句结合使用:
drop table if exists testdb.with_insert_into;
create table testdb.with_insert_into (id int);
with test_data as
(select 3 id)
insert into testdb.with_insert_into (id)
select * from test_data;
我正在使用 MySQL 8.0.41 版本。
它输出错误:
错误代码:1064。您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在第 3 行“insert into testdb.with_insert_into (id) select * from test_data”附近使用的正确语法
你已经接近了。
INSERT INTO
需要先来,然后是WITH
后。看起来和感觉起来都很奇怪,但这是正确的顺序。这有效: