[英]PHP MySQL Select * from 2 different tables and display the data mixed together ordered by datetime column in both tables
[英]PHP /MySQL Select data from 3 different tables and display result it in one table
我的数据库中有3个表
这是students_absence表
---------------------------------------------------------------------
| tag_id | lecture_name | abs_hours | abs_ratio | days |
|-----------+---------------+-----------+---------------+-----------|
| 00000023 | 1 | 8 | 5.3333 | 13/4/2014 |
---------------------------------------------------------------------
和第二名的学生
---------------------------------------------------------------------------------
| tag_id | name | collage | department | class | gender | |
|---------------+-----------+---------------+---------------+-------+-----------+
| 00000023 | John Deem | Engineering | Computer | first | male |
---------------------------------------------------------------------------------
第三表讲台
-------------------------------------------------------------
| id | lecture_name | lecturer_name | start | end |
|-------+---------------+-------------------+-------+-------+
| 1 | Math 1 | Dr. Anas Ali | 8 | 10 |
-------------------------------------------------------------
因此,我需要通过students_absence表中的tag_id(00000023)进行查询,并获取此tag_id,并从students表中获取有关此表的完整信息。 并从students_absence中获取(lecture_name = 1)并将其与story_table中的(id = 1)进行比较,并从中获取完整的信息,具体取决于id
最终结果应该是这样
---------------------------------------------------------------------------------
| tag_id | name | lecture_name | lecturer_name | abs_hours |
|---------------+-----------+---------------+-------------------+---------------+
| 00000023 | John Deem | Math 1 | Dr. Anas Ali | 8 |
---------------------------------------------------------------------------------
我无法查询任何人都可以向我提供以上表格?
(未测试)
select s1.tag_id s2.name,s3.lecture_name,s1.abs_hours from students_absence s1 join second_table s2 on s2.tag_id="00000023" and s1.tag_id="00000023" join lecture_table s3 on s1.lecture_name =s3.lecture_name
我假设在第一个表中,您的意思是讲课ID,而不是讲课名称。 如果我理解正确,则输入为和,并且您想知道某个班级缺少一名学生多少小时。 然后,您需要的查询如下所示:
SELECT students.tag_id, name, lecture_name, lecturer_name, abs_hours FROM students_absence JOIN students on students_absence.tag_id = students.tag_id JOIN lecture_table on students_absence.lecture_id = lecture_table.id WHERE students.tag_id=<STUDENT_ID> AND lecture_table.id = <LECTURE_ID>
如果您需要所有班级所有学生的清单,只需省略“ WHERE”部分,然后省略所有内容
尝试:
SELECT sa.tag_id ,s.name ,lt.lecture_name,lt.lecturer_name,sa.abs_hours
FROM students_absence sa
INNER JOIN students s ON s.tag_id = sa.tag_id
INNER JOIN lecture_table lt ON lt.id = sa.lecture_name
WHERE sa.tag_id= '00000023'
SQL命令:
SELECT s.tag_id, s.name, l.lecture_name, l.lecturer_name, sa.abs_hours FROM students_absence sa JOIN students s USING (tag_id) JOIN lecture_table l ON (sa.lecture_name = l.id) WHERE sa.tag_id = '00000023'
您需要使用加入
SELECT s.tag_id, s.name, lt.lecture_name, lt.lecturer_name, sa.abs_hours
FROM students_absence sa
JOIN students s ON (s.tag_id = sa.tag_id)
JOIN lecture_table lt ON (s.lecturer_name = lt.id)
WHERE s.tag_id = 00000023;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.