我的任务是从另一个表中为每个ID插入3个随机数据 我的语法错误
set @num := 0, @type := '' ,@stat :='';
INSERT INTO random
as
(
SELECT
*
FROM (
select userID,userNAME, chaID, chaNAME,goal,gender,
@num := if(@type = userID, @num +1,1) as row_number,
@type := userID as dummy,
@stat as status
from userchar
order by userID
) as x where x.row_number <= 3)
我将把这段代码放在事件调度程序中,以便在每日
中插入新数据1064 - 您的SQL语法出错;检查手册 对应于您的MariaDB服务器版本,以获得正确的语法 靠近'INSERT INTO random as(SELECT * FROM(select userID,userNAME, chaID,c'在第2行
非常感谢你的每一个建议。
答案 0 :(得分:0)
我怀疑问题是试图同时运行多个语句。您可以通过初始化查询本身中的变量来解决此问题:
INSERT INTO random( . . . )
select u.*
from (select userID, userNAME, chaID, chaNAME, goal, gender,
(@num := if(@u = userID, @num +1,
if(@u := userId, 1, 1)
)
) as row_number,
userID as dummy,
@stat as status
from userchar u cross join
(select @u = '', @num := 0, @stat := '') params
order by userID, rand()
) u
where u.row_number <= 3;
还有其他一些问题:
insert
时,请始终列出列。如果您正在学习SQL,这一点尤为重要,因此您将学习良好的习惯。select
中表达式的评估顺序,因此可以按任意顺序评估表达式。rand()
。不确定&#34;不确定&#34;和&#34;随机&#34;。