0

Table: students

studentid | rollNo | name

Table: classRecord

recordId | studentId | isPresent

students.studentid is the foreign key for classRecord.studentId

I have the value of the rollNo from the students table and also whether he was present in class or not and I want to get the studentid from the students table and insert it into classRecord. I want to first get the value of studentid for a rollNo and then Insert this value of studentid into classRecord table.

7
  • You can use the PHP Method to get an associative array? I'm a bit confused as to what your question title has to do with the body Commented Feb 17, 2017 at 14:43
  • 1
    How the question is related to the title? Commented Feb 17, 2017 at 14:44
  • Sorry. Forgot to change the title. It is based only on pure sql Commented Feb 17, 2017 at 14:48
  • Please clarify what you are trying to do. Commented Feb 17, 2017 at 14:53
  • 1
    What's stopping you? Commented Feb 17, 2017 at 14:55

1 Answer 1

3

You need to do a subquery to select the student ID from the student table, and insert it into the classRecord table:

INSERT INTO classRecord (studentId, isPresent)
    SELECT student.studentId, <is_present_value> 
    FROM student 
    WHERE student.rollNo = <rollNo_value>;
Sign up to request clarification or add additional context in comments.

3 Comments

Added some explanatory text.
Do you need to insert three column values into a table with three columns? Or specify that you're skipping the recordID column by listing the other two: INSERT INTO classRecord(studentId, isPresent) SELECT …?
Oops. You are correct. I have modified this answer to reflect this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.