我的数据库中有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 |
---------------------------------------------------------------------------------
和第3桌讲座
-------------------------------------------------------------
| id | lecture_name | lecturer_name | start | end |
|-------+---------------+-------------------+-------+-------+
| 1 | Math 1 | Dr. Anas Ali | 8 | 10 |
-------------------------------------------------------------
所以我需要在students_absence表中通过tag_id(00000023)进行查询并获取此tag_id并从学生表中获取有关它的完整信息。从students_absence中取出(讲座名= 1)并在讲课时将其与(id = 1)进行比较,并从中获取完整信息取决于id
最终结果应该是这样的
---------------------------------------------------------------------------------
| tag_id | name | lecture_name | lecturer_name | abs_hours |
|---------------+-----------+---------------+-------------------+---------------+
| 00000023 | John Deem | Math 1 | Dr. Anas Ali | 8 |
---------------------------------------------------------------------------------
我无法得到一个查询给我上面的表,任何人都可以提供建议吗?
答案 0 :(得分:0)
(未经测试)
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
答案 1 :(得分:0)
我假设在第一个表中你的意思是讲话而不是讲座名。 如果我理解正确,那么输入就是并且您想知道某个班级学生失踪了多少小时。 然后你需要的查询是这样的:
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>
如果您需要所有班级中所有学生的列表,请省略&#34; WHERE&#34;部分和之后的一切
答案 2 :(得分:0)
试试:
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'
答案 3 :(得分:0)
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'
答案 4 :(得分:0)
您需要使用join
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;