JONAS VINCENT Samson
JONAS VINCENT Samson

Reputation: 11

Insert Values select PHP

INSERT INTO grade7 (studentid,section) VALUES (SELECT MAX(studentid)  FROM student_profile limit 1), '----';

What is wrong with my code, tried it individually it works

Upvotes: 0

Views: 28

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520908

I'm not familiar with using a subquery as the predicate of VALUES during an insert. More typical would be to use INSERT INTO grade7 ... SELECT:

INSERT INTO grade7 (studentid, section)
SELECT MAX(studentid), '----'
FROM student_profile

If you are doing this work directly on MySQL, then you might also be able to use MySQL's LAST_INSERT_ID() function, which returns the auto increment value from the most recent insert. If you had just done the insert on student_profile, then you could have simply used this to insert into grade7:

INSERT INTO grade7 (LAST_INSERT_ID(), '----')    

Upvotes: 1

Manngo
Manngo

Reputation: 16281

Your subquery needs a pair of parentheses, but your VALUES also needs parentheses.

Try this:

INSERT INTO grade7 (studentid,section)
VALUES ((SELECT MAX(studentid) FROM student_profile limit 1), '----');

Upvotes: 0

Related Questions