0

我有两张表,一张存储注册的学生,我们称之为student_register 。第二个表保存学生的考试详细信息,我们称之为exam_details。在 students_register 表中,我存储:

student registration number    
first name    
last name    
email_address    
date_of_birth

和其他细节。

在exam_details 表中,我存储了不同科目的注册号和学生分数。

现在,挑战是我想查询exam_details表并在表中显示数据,而不是显示学生注册号,我想将exam_details 表中的注册号与students_register 表中的注册号相关联,以便我可以显示学生的姓名而不是注册号。

我该怎么办?

1.students_register 表

id reg_number first_name  last_name  email_address      dob
1   P2894      John        Smith       john@example.com   12/05/1990

2.考试明细表

id reg_number english maths chemistry  biology physics
1  P2894       60%    80%    50%         72%     64%

我如何在表格中显示这些数据,以便我拥有

first_name last_name  english  maths  chemistry  biology physics
 John       Smith      60%      80%    50%        72%    64%
4

8 回答 8

2
SELECT tb2.first_name, tb2.last_name, tb1.english, tb1.maths, tb1.chemistry, tb1.bilogy, tb1.physics
FROM exam_details AS tb1
INNER JOIN students_register AS tb2
ON tb1.reg_number = tb2.reg_number

看看 SQL Joins -> http://www.w3schools.com/sql/sql_join.asp

于 2013-04-17T08:12:44.550 回答
1

使用简单的 JOIN 查询:

SELECT id, student_register.reg_number, first_name, 
       last_name, english, maths, chemistry, etc 
FROM student_register
JOIN exam-details ON student_register.reg_number = exam-details.reg_number
于 2013-04-17T08:12:28.567 回答
1

没有必要在JOIN这里使用。我认为这是最简单的方法。

SELECT 
  sr.first_name, 
  sr.last_name, 
  ed.english, 
  ed.maths, 
  ed.chemistry, 
  ed.biology, 
  ed.physics 
FROM 
  students_register sr, 
  exam_details ed 
WHERE 
  sr.reg_number = ed.reg_numver
于 2013-04-17T08:13:05.960 回答
1

见为JOIN

SELECT first_name, last_name, english, maths, chemistry, biology, physics FROM exam_details AS ex JOIN students_register st ON (ex.reg_number = st.reg_number)
于 2013-04-17T08:14:02.633 回答
1
SELECT * FROM student-register INNER JOIN exam-details ON student-detail.reg_number=exam-details.reg_number

可能是您的疑问。然后你打印出你喜欢的任何东西。

于 2013-04-17T08:15:28.307 回答
0
select first_name, last_name, english, maths, chemistry, biology, physics
from students_register, exam_details
where students_register.reg_number=exam_details.reg_number
于 2013-04-17T08:12:14.753 回答
0

你必须加入这两张桌子

SELECT s.first_name, s.last_name, e.english, e.maths, e.chemistry; e.biology, e.physics FROM students_register s LEFT JOIN exam_details e USING (reg_number)
于 2013-04-17T08:12:38.440 回答
0

可能这会帮助你。

选择 students_register.first_name,students_register.last_name,exam-details.english,exam-details.maths,exam-details.chemistry,exam-details.biology,exam-details.physics 从exam-details left join students_register on students_register.reg_number=考试详细信息.reg_number

于 2013-04-17T08:17:02.023 回答