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
精彩评论