普通网友 2018-09-12 07:32
浏览 57
已采纳

我有三列,我想将它们组合在一个main_table中

i am working in mysql, i know about tables relationships 'joins' etc, I have three different tables, each table has column id, i want to combine all three ids in one main table, because i want to apply inner join after getting all ids in one table. can you please tell me how i can get all three ids in one table, i know about foreign key and primary key, but i don't know how i can apply that here... or is there any simple method of doing it.enter image description here My tables name:

Table 1 contains: student_id coloumn
Table 2 contains: teachers_id
Table 3 contains: class_id

Main table is table 4: which will have student_id,teachers_id,class_id coloumn

i am trying to generate time table, i want get student id,teachers_id and class_id, in main table time_table; i am trying to do normalization so that i don't have to repeat all name again and again, i can just use id to call any class name teacher name subject name etc, all ids are primary keys in tables.

The relationship is one to one in this case


i am working on php_mysql.

Thankyou

  • 写回答

3条回答 默认 最新

  • douzhi4311 2018-09-12 08:38
    关注

    Create tables as follows,

     create table subject(subject_id int primary key,sub_name varchar(20))
     create table teacher(teacher_id int primary key,teacher_name varchar(20))
     create table class(class_id int primary key,class_sec varchar(20))
    
     create table timetable(t_id int primary key,subject_id int references 
     subject(subject_id)
     ,teacher_id int references teacher(teacher_id),class_id int references 
     class(class_id))
    

    Inserting sample values

     insert into subject values(1,'Tamil')
     insert into teacher values(1,'Pugal')
     insert into class values(1,'12th A')
     insert into timetable values(1,1,1,1)
    

    Using Inner join to connect tables,

     select s.sub_name,t.teacher_name,c.class_sec from timetable t1 
     inner join subject s
     on s.subject_id = t1.subject_id inner join teacher t
     on t.teacher_id = t1.teacher_id inner join class c
     on c.class_id   = t1.class_id
    

    Try this...And revert me if any clarifications needed..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作