How to insert values into a table from two different tables?
You would write the query like this
insert into classroom (date, teacher_id, student_id)
select '2014-07-08', t.id, s.id
from teachers t,students s
where t.teacher_name = 'david'
and s.student_name = 'sam';
Be careful. This is a Cartesian product. Another way to approach this is
select teacher_id into @tid from teachers where teacher_name = 'david';
select student_id into @sid from students where student_name = 'sam';
insert into classroom (date, teacher_id, student_id) values ('2014-07-08',@tid,@sid);
The easiest way you can do this is using sub queries:
INSERT INTO classroom(teacher_id,student_id)
VALUES ((SELECT id FROM students WHERE s_name='sam'),
(SELECT id FROM teacher WHERE t_name='david'));