简体   繁体   English

从mysql中的两个表中获取字段

[英]Fetch fields from two tables in mysql

I have two tables, one which stores the registered students, lets call it students_register . 我有两个表,其中一个存储已注册的学生,让我们将其称为“ students_register” The second table keeps examination details of the students, lets call it exam_details . 第二张表保留了学生的考试详细信息,将其称为Exam_details In the students_register table, i store: 在students_register表中,我存储:

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. 在exam_details表中,我将注册编号和学生分数存储在不同的主题中。

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. 现在的挑战是,我要查询的exam_details表,在表中,但不是显示学生的注册号显示的数据,我想在exam_details表中的注册号在students_register表关联到这样我可以显示学生的姓名而不是注册号。

How can i go about this? 我该怎么办?

1. students_register table 1. students_register表

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

2. exam-details table 2.考试详情表

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 看看SQL Joins-> http://www.w3schools.com/sql/sql_join.asp

With a simple JOIN query: 使用简单的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

There is no need to use JOIN here. 此处无需使用JOIN 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 从考试详细信息中选择“ students_register.first_name”,“ students_register.last_name”,“考试详细信息”,“英语”,“考试详细信息”,“数学”,“考试详细信息”,“生物学”,“考试详细信息”。考试详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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