Mysql 在插入语法问题中选择子查询中的变量还是什么?

Mysql 在插入语法问题中选择子查询中的变量还是什么?,mysql,insert,subquery,syntax-error,Mysql,Insert,Subquery,Syntax Error,我构造了一个insert,它执行许多操作来计算要插入的值。然而,第一个值只是一个标量,一个不变的id,它与所有要计算然后插入的记录/字段相关联。该标量值取自一个表,而不是从中提取或计算所有其他值的表 这个值也应该分配给一个变量,我认为应该可以在insert的select表达式中这样做。我没能做到这一点,我也不知道哪里出了问题,是我的语法出了错误,还是我试图做一些非法的事情。当我在一个单独的语句中完成这个任务时,它是有效的。就在我将这些陈述组合在一起时,如下所示: 这很好(我省略了大部分计算):

我构造了一个insert,它执行许多操作来计算要插入的值。然而,第一个值只是一个标量,一个不变的id,它与所有要计算然后插入的记录/字段相关联。该标量值取自一个表,而不是从中提取或计算所有其他值的表

这个值也应该分配给一个变量,我认为应该可以在insert的select表达式中这样做。我没能做到这一点,我也不知道哪里出了问题,是我的语法出了错误,还是我试图做一些非法的事情。当我在一个单独的语句中完成这个任务时,它是有效的。就在我将这些陈述组合在一起时,如下所示:

这很好(我省略了大部分计算):

但这并不是:

insert into um (id_universe, pid, $vol)
select (select id into @univ_num from u order by id desc limit 1), pid, 
cv/@univ_len as $vol...from..where..etc...
解析器不喜欢“into”:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual...
for the right syntax to use near 'into @univ_num from u order by id desc limit 1), pid, cv/@univ_len as $vol' at line 2
如果没有“into”,解析器不会介意它;如果没有“into@univ_num”,子查询和查询一起就可以了,即

insert into um (id_universe, pid, $vol)
select (select id from u order by id desc limit 1), pid, cv/@univ_len as $vol

很好。TIA在MySQL文档中没有看到它,但是,这个表单可以使用赋值运算符(第3行):

insert into um (id_universe, pid, $vol)
select (select id from u order by id desc limit 1), pid, cv/@univ_len as $vol
select @univ_num, pid, cv/@univ_len as $vol
 from
   (select  @univ_num := (select id from univ_members order by id desc limit 1) a) b,
   (select pid as pid, close as close, vol as vol, sum(d.close * d.vol) as cv
   from ... -- from the debug_data_1 table
     where... ) x;