简体   繁体   中英

Fetch fields from two tables in mysql

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       john@example.com   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%
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

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

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

参见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)
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.

select first_name, last_name, english, maths, chemistry, biology, physics
from students_register, exam_details
where students_register.reg_number=exam_details.reg_number

您必须加入这两个表

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM