INSERT INTO使用其他表中的数据导致错误1064

时间:2019-06-05 15:29:12

标签: mysql select myisam insert-into

我对INSERT INTO的命令是:

INSERT INTO environment_fid (FID_environment) VALUES SELECT ID FROM environment WHERE name='planes';

但是我得到了

  

错误1064(42000):您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行的“从环境WHERE名称='planes'中选择ID”附近使用

我有三个表,但是我只尝试了两个:

CREATE TABLE `environment_fid` (
  `ID` mediumint(9) unsigned AUTO_INCREMENT,
  `FID_environment` mediumint(9) unsigned,
  `FID_monster` mediumint(9) unsigned ,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM;

DROP TABLE IF EXISTS `environment`;
CREATE TABLE `environment` (
  `ID` mediumint(9) unsigned AUTO_INCREMENT,
  `name` text NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM;

但是,当我放入零件时:

INSERT INTO environment_fid (FID_environment) VALUES (78);

查询正常,受影响1行(0.00秒)

 SELECT ID FROM environment WHERE name='planes';

*+----+
| ID |
+----+
| 66 |
+----+

1 row in set (0.00 sec)*

我不知道这个问题,有什么建议吗?

谢谢

INSERT INTO environment_fid (FID_environment) SELECT ID FROM environment WHERE name='planes';

解决我的问题,但是如何获得其他人的桌子?例如:

INSERT INTO environment_fid (FID_environment, FID_monster) 
SELECT ID FROM environment  WHERE name='planes',
SELECT ID FROM monster WHERE family='Blue';

3 个答案:

答案 0 :(得分:2)

删除<div id="container"> <a href="https://www.hoohle.com"> <h2>This text goes over a floated div - This text goes over a floated div - This text goes over a floated div</h2> </a> <div class="floatedconteiner">My floated container<br> <span class="myspan">2019</span> </div> </div>,然后 将查询更改为:

values

INSERT INTO environment_fid (FID_environment) 
SELECT ID FROM environment WHERE name='planes';

答案 1 :(得分:1)

删除VALUES中的INSERT INTO。您不能在VALUES

中将SELECTINSERT INTO混合在一起

因此您的工作查询将是:

INSERT INTO environment_fid (FID_environment) 
SELECT ID FROM environment WHERE name='planes';

INSERT INTO语句的一些示例

INSERT INTO TableName (Col1, Col2, Col3) VALUES (@Val1, @Val2, @Val3)

INSERT INTO TableName (Col1, Col2, Col3) 
SELECT @Val1, @Val2, @Val3

答案 2 :(得分:0)

我发现了我的最后一个问题:

INSERT INTO environment_fid (FID_environment, FID_monster) 
SELECT 
 (SELECT environment.ID FROM environment WHERE name='planes') AS FID_environment, 
 (SELECT ID FROM monster WHERE family='Blue') AS FID_monster;