开发者

Error 1064 - SQL syntax in MySQL for an INSERT statement [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily na开发者_JAVA技巧rrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

i am trying this query but didn't work.

ERROR: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= (SELECT id_university FROM university WHERE university = "xxx") , `singl' at line 2

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" ) 

How can this be solved?


Unless MySQL supports a very strange syntax, that's not how you write an INSERT statement. It takes this form:

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

So you would have

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'
)

Caveat: I'm not sure whether MySQL will actually handle a nested SELECT for each column value, but the above is how INSERT statements are typically written.


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

More here http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜