如何从另一个表中等于两个不同的ID?

时间:2017-12-07 05:08:29

标签: php mysql

select student.student_fname,
student.student_mname,
student.student_lname, logs.time, logs.transaction,logs.entity_type,prof.professor_fname,prof.professor_mname,prof.professor_lname 
from student_tbl as student,
logs_tbl as logs,
professor_tbl as prof 
where student.student_id=logs.entity_id 
and prof.professor_id=logs.entity_id
and logs.date ='$date_today' 
and logs.course_id='$course' 
and logs.sections_id='$sections'
and logs.rooms_id='$room'  
group by logs.date, 
logs.term, 
logs.course_id, 
logs.entity_id  
order by logs.logs_id asc 

在student_tbl和professor_tbl中,我的student_id和professor_id等于logs_tbl的entity_id。我如何将不同表中的两个id与logs_tbl中的entity_id进行比较?这是唯一给我错误的一条线。或者你有建议吗?除了sql注入的东西

更新

我有 student_tbl 列:student_id,student_fname,student_mname,student_lname。我也有教授_tbl,其中有以下列: professor_id ,professor_fname,professor_mname,professor_lname和我的第三个表,它的所有基础是 logs_tbl 列:entity_id (这是student_id和professor_id将等同的地方),course_id,rooms_id,sections_id,time,date,transaction,entity_type。 这就是我想要展示的内容:

  • student_fname / professor_fname(取决于entity_id)
  • student_mname / professor_mname(取决于entity_id)
  • student_lname / professor_lname(取决于entity_id)
  • 时间
  • 交易
  • ENTITY_TYPE

示例数据显示:

  • Kris | Consta |罗卡| 11:30 |在|学生
  • 鲍勃|琼斯|马利| 11:32 |在|学生
  • 罗纳德|詹姆斯|莱昂| 11:35 |在|教授
    • Kris | Consta |罗卡| 1:00 |出|学生
  • 鲍勃|琼斯|马利| 1:05 |出|学生
  • 罗纳德|詹姆斯|莱昂| 1:06 |出|教授

2 个答案:

答案 0 :(得分:3)

  select student.student_fname,  student.student_mname, student.student_lname, logs.time, logs.transaction,logs.entity_type
          from student_tbl as student inner join logs_tbl as logs on student.student_id=logs.entity_id  
     where   logs.date ='$date_today' 
                and logs.course_id='$course' 
                and logs.sections_id='$sections'
                and logs.rooms_id='$room'  
                group by logs.date,  logs.term, 
                logs.course_id, 
                logs.entity_id   


    UNION  

      select prof.professor_fname,prof.professor_mname,prof.professor_lname , logs.time, logs.transaction,logs.entity_type
          from   professor_tbl as prof inner join logs_tbl as logs   on prof.professor_id=logs.entity_id
             where   logs.date ='$date_today' 
                and logs.course_id='$course' 
                and logs.sections_id='$sections'
                and logs.rooms_id='$room'  
                group by logs.date,  logs.term, 
                logs.course_id, 
                logs.entity_id   

答案 1 :(得分:0)

select student.student_fname,student.student_mname,student.student_lname, logs.time, logs.transaction,logs.entity_type,prof.professor_fname,prof.professor_mname,prof.professor_lname 
from student_tbl as student
join logs_tbl as logs on student.student_id=logs.entity_id and logs.date ='$date_today' and logs.course_id='$course' and logs.sections_id='$sections' and logs.rooms_id='$room'
join professor_tbl as prof on prof.professor_id=logs.entity_id
group by logs.date, logs.term, logs.course_id, logs.entity_id 
order by logs.logs_id asc

您可以使用join