简体   繁体   中英

How to insert into a row, a value from other table?

I have 3 Mysql tables.

A table with the classes and the labs and their id.

A table with the teachers_list and their subject. A table which is going to be the schedule.**

I want to randomly assign one of the physicists to one of the physics labs on my third table which is going to be the schedule.

INSERT INTO schedule(teacher_name, class_id)
VALUES (select teacher_name from teachers_list where subject="Physicist” order by rand() limit 1, 

select id from lab_list where lab="Physics_lab" order by rand() limit 1);

**This one doesn't work:(

Can you help me?**

I think that you want the insert... select syntax, along with a subquery:

insert into schedule(teacher_name, class_id)
select 
    (
        select teacher_name 
        from teachers_list 
        where subject = 'Physicist'
        order by rand()
        limit 1
    ),
    id
from lab_list
where lab = 'Physics_lab'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM