0

I have two tables, one which stores the registered students, lets call it students_register. The second table keeps examination details of the students, lets call it exam_details. In the students_register table, i store:

student registration number    
first name    
last name    
email_address    
date_of_birth

and other details.

In the exam_details table, i store the registration number and the students marks in the different subjects.

Now, the challenge is that i want to query the exam_details table and display the data in a table but instead of displaying the students registration number, i want to associate the registration number in the exam_details table to that in the students_register table so that i can display the student's names instead of the registration number.

How can i go about this?

1. students_register table

id reg_number first_name  last_name  email_address      dob
1   P2894      John        Smith       [email protected]   12/05/1990

2. exam-details table

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

How do i display this data in a table such that i have

first_name last_name  english  maths  chemistry  biology physics
 John       Smith      60%      80%    50%        72%    64%
2
  • 1
    It's called a "join". Commented Apr 17, 2013 at 8:11
  • 1
    what about mysql join tables -dev.mysql.com/doc/refman/5.0/en/join.html - from your sample key would be reg_number like <<SELECT sr.first_name, sr.last_name, ed.english, ed.maths, ed.chemistry, ed.biology, ed.physics from [exam-details] ed LEFT JOIN [students_register] sr ON sr.reg_number = ed.reg_number;>> Commented Apr 17, 2013 at 8:14

8 Answers 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

Take a look at SQL Joins -> http://www.w3schools.com/sql/sql_join.asp

Sign up to request clarification or add additional context in comments.

Comments

1

With a simple JOIN query:

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

Comments

1

There is no need to use JOIN here. I think this is the easiest way.

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

2 Comments

This is a join, too. Just another way of writing it. It will result in the same execution plan as a 'regular JOIN'.
Have actually learnt a new way of joining tables. Your answer works well, Thanks indeed.
1

See for 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)

1 Comment

This works well too. Thanks for the immediate answers. Am overwhelmed.
1
SELECT * FROM student-register INNER JOIN exam-details ON student-detail.reg_number=exam-details.reg_number

may be your query. Then you print out whatever you like.

Comments

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

Comments

0

You have to join these 2 tables

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)

Comments

0

May be this will help u.

select students_register.first_name,students_register.last_name,exam-details.english, exam-details.maths, exam-details.chemistry, exam-details.biology, exam-details.physics from exam-details left join students_register on students_register.reg_number=exam-details.reg_number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.