Association query between several tables in the database

zhaozj2021-02-16  50

In the database operation, there are many times not to perform database operations for a table, and usually also pass the values ​​of the fields of other tables. However, this even a table is not related. Usually by indexing or health

As follows as an example.

Table I. Table name TB1. Where STUDENTID is the main index. Extract.

StudentId StudentName Studentgender StudNtage 3102079014 Liu XX Male 25 3102079015 Zhao Moumou 23

Table II. Table name TB2. Where TeacherID is the main index. Extract.

Teacherid Teachername TeacherManageStudent TeacherMemo 3645221 Liu Gang 3102079014 None 3645222 Chengkun 3102079015 None

Now there is a query, you need to know the name of the student manager to manage. It can be known from the table two to know that the teacher Liu Gang is 3102079014 this student, and it is known to Table 1 3102079014 The name of this student is Liu XX. How to write this query statement?

as follows:

SQL = "SELECT TB2.TEACHERNAME, TB1.STUDENTNAME for TB2, TB1 WHERE TB2.TEACHERMANAGESTUENT = TB1.STUDENTID"

The result of coming out is:

Teachername StudentName Liu Gang Liu XX

Maybe readers will now produce a question, in Table 2. TEACHERID as an index, inevitably only one ID record. So how do I manage all students?

From two processes. 1: In the TeacherManagestudent field, all the students' IDs are recorded in. Middle! Or other symbols are indicated, when the SQL generation is generated, use a loop to constantly write all the students to the IDs to SQL.:2 :tb2 Do not build the above form, put the teacher's basic information in TB2. Another new table is placed in the teacher's ID and the teacher management student ID record, as follows:

New Table: Table Name TB3

Teacherid TeacherManagestudent 3645221 3102079014

Table 2 turns into

Teacherid Teachername TeacherMemo 3645221 Liu Gang None No 364522 成昆 无

转载请注明原文地址:https://www.9cbs.com/read-22100.html

New Post(0)