SQL语法错误 - INSERT中的SELECT语句

时间:2014-12-09 04:45:35

标签: mysql syntax

我有这行sql,它在select语句的右括号后面给出了语法错误。我相信我可以用这种方式编写代码。还有其他选择吗?

select语句中的变量来自GET,其他变量来自POST。任何帮助表示赞赏。

$sql2="INSERT INTO playerRegSeason 
       (playerID, year, teamID, gp, minutes, pts, oreb, dreb, reb, 
        asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)
VALUES ((
       SELECT playerID FROM players WHERE firstname='$firstname' 
       AND lastname='$lastname' AND firstseason=$firstseason), $year, 
      '$team', $gp, $minutes, $pts, $oreb, $dreb, $reb, $asts, $stl, 
       $blk, $turnover, $pf, $fga, $fgm, $fta, $ftm, $tpa, $tpm)";

5 个答案:

答案 0 :(得分:2)

当您从选择中插入时,请不要使用VALUES

INSERT INTO abc (foo, bar)
SELECT x, y FROM z

答案 1 :(得分:0)

使用此功能。

$sql2="INSERT INTO playerRegSeason 
       (playerID, year, teamID, gp, minutes, pts, oreb, dreb, reb, 
        asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)

       SELECT playerID, $year, '$team', $gp, $minutes, $pts, $oreb, 
         $dreb, $reb, $asts, $stl, $blk, $turnover, $pf, $fga, $fgm, 
         $fta, $ftm, $tpa, $tpm FROM players WHERE firstname='$firstname' 
       AND lastname='$lastname' AND firstseason=$firstseason";

答案 2 :(得分:0)

insert into table1 (field1, field2, ...)
select col1, col2, ...
from table2
where ....

答案 3 :(得分:0)

您编写的SQL语句不正确,您需要删除values子句,或者可以参考Insert with select来学习如何使用select语句编写插入

答案 4 :(得分:0)

使用此查询代替其正在运行的查询

$ sql2 =“INSERT INTO playerRegSeason(playerID,year,teamID,gp        ,分钟,pts,oreb,dreb,reb,asts,stl,blk,营业额,pf,fga,fgm,fta,ftm,tpa,tpm)

    values(SELECT playerID,year, $team, $gp, $minutes, $pts, $oreb, $dreb, 
    $reb, $asts, $stl, $blk, $turnover, $pf, $fga, $fgm, $fta, $ftm, $tpa, $tpm
    WHERE firstname='$firstname' 
    AND lastname='$lastname' AND firstseason='$firstseason')";