错误1064 - MySQL中用于INSERT语句的SQL语法

时间:2011-06-13 18:06:34

标签: mysql mysql-error-1064

我正在尝试此查询,但无效。

  

错误:#1064 - 您的SQL语法出错;查看与MySQL服务器版本对应的手册,以便在'=(SELECT id_university FROM university WHERE university =“xxx”)附近使用正确的语法,在第2行使用`singl'

INSERT INTO `database_charts`.`university_has_single_user` (
        `university_id_university` = (SELECT id_university FROM university WHERE university = "xxx") , 
        `single_user_users_id_user` = (SELECT users_id_user FROM single_user WHERE users_id_user = "1" ), 
        `course_id_course`= (SELECT id_course FROM course WHERE course = "yyy"),
        `year`= "2000" ) 

如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

除非MySQL支持非常奇怪的语法,否则您不会编写INSERT语句。它采用以下形式:

INSERT INTO <TableName> (<Column1,Column2,..,ColumnN>) VALUES (<Value1,Value2,..,ValueN>)

所以你会有

INSERT INTO database_charts.university_has_single_user (
  university_id_university, 
  single_user_users_id_user, 
  course_id_course,year) 
VALUES (
  (SELECT id_university FROM university WHERE university = 'xxx'),
  (SELECT users_id_user FROM single_user WHERE users_id_user = '1' ),
  (SELECT id_course FROM course WHERE course = 'yyy'),
  '2000'
)

警告:我不确定MySQL是否会为每个列值实际处理嵌套的SELECT,但上面的 通常是如何编写INSERT语句的。

答案 1 :(得分:0)

INSERT INTO TABLE2 (TABLE2_FIELD)
  SELECT TABLE1_FIELD
  FROM TABLE1 
  WHERE TABLE1_SOMEFIELD IS 'SOMETHING'

更多http://dev.mysql.com/doc/refman/5.6/en/insert-select.html