I want to insert a value to a table where parent value for all values being inserted is some entry from the same table "master
".
I used the subquesry with aliases as suggested on INSERT INTO with SubQuery MySQL
INSERT INTO mdl_question_categories (NAME, stamp, parent, info)
SELECT
'cat100' AS NAME,
'localhost+140117065545+AUXF' AS stamp,
id
FROM
mdl_question_categories
WHERE NAME = 'master',
'desc' AS info ;
This code throws an error
Error: 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 ''desc' as info' at line 1
SQLState: 42000
ErrorCode: 1064
Then I changed the query to
INSERT INTO mdl_question_categories (NAME, stamp, info, parent)
SELECT
'cat100' AS NAME,
'localhost+140117065545+AUXF' AS stamp,
'desc' AS info,
id
FROM
mdl_question_categories
WHERE NAME = 'master' ;
This works excellent.
Now the question is why did this happen?
Is there any restriction that we should always keep the value picking sql part id from mdl_question_categories where name='master'
at the end of query part?