简体   繁体   English

如何将其他表中的值插入一行?

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

I have 3 Mysql tables.我有 3 个 Mysql 表。

A table with the classes and the labs and their id.包含课程和实验室及其 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... select语法以及子查询:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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