多表的mysql/sql查询

hjzp0vay  于 2021-06-19  发布在  Mysql
关注(0)|答案(3)|浏览(395)

我有多个表,我试图做一个查询搜索学生的ID“studentid”。当我运行下面的查询时,mysql或sqlserver冻结(正在加载…)。有什么建议吗?并不是所有的表都列出了。谢谢。
table:
学生

•   studentsID  Int Pkey
•   Fname   string
•   Lname   string …    
•   studentsGender  Int Fkey
•   studentsGender  int Fkey
•   studentsRezAddressID    int Fkey
•   studentsBirthID int Fkey
•   studentsParentsID   int Fkey

学生地址:

•   RezAddressID    Int Pkey
•   RezStreet   Varchar
•   RezCity Int Fkey
•   RezState    int Fkey
•   RezDistrict int Fkey
•   RezCountry  int Fkey
•   RezZipcode  int

学生教育:

•   eduID   Int Pkey
•   eduAcadYear Int Fkey
•   eduAdmitionTest Varchar
•   scholarship Varchar
•   study   Int Fkey
•   classroom   Int Fkey
•   studentStatus   Int Fkey
•   testStatus  Int Fkey
•   additionalInfo  Varcha

r
学年

academicID  Int Pkey
academicYear    VarChar

城市

cityID  Int Pkey
city    VarChar

国家

countryID   Int Pkey
country VarChar

国家

districtsID Int Pkey
district    VarChar

种族

ethnicityID Int Pkey
ethnicity   VarChar

毕业生

gradID  Int Pkey
grad    VarChar

SELECT
students.studentsID,
students.studentsLname,
students.studentsMname,
students.studentsFname,
students.studentsMobile,
students.studentEmail,
students.joined_date,
genders.gender,
ethnicities.ethnicity,
races.races,
student_address.RezStreet,
student_address.RezZipcode,
grads.grad,
rooms.rooms,
studies.study,
student_edu.additionalInfo,
student_status.student_status,
academic_years.academicYear,
teststatus.testStatus,
student_edu.scholarship,
student_birth_place.birthDate,
cities.city,
states.states,
countries.country,
districts.district   
FROM
    (
        students,
        student_edu,
        student_address,
        studies,
        rooms,
        student_status,
        teststatus,
        academic_years,
        grads,
        races,
        genders,
        ethnicities,
        student_birth_place,
        districts,
        cities,
        states,
        countries
    )
WHERE students.studentsID = “00012” ;

我也使用了join语句,但问题仍然存在。 INNER JOIN grads As studGrad ON studentsEduID = student_edu.eduID INNER JOIN studies AS studyOption ON student_edu.study = studies.studiesID INNER JOIN rooms AS classRoom ON student_edu.classroom = rooms.roomsID INNER JOIN student_status AS stat ON student_edu.studentStatus = student_status.student_statusID INNER JOIN testStatus AS admiTest ON student_edu.testStatus = testStatus.testStatusID INNER JOIN student_address AS studAddre ON students.studentsRezAddressID = student_address.RezAddressID INNER JOIN student_birth_place AS stubBirth ON students.studentsBirthID = student_birth_place.birthID INNER JOIN cities AS ville ON student_address.RezCity = cities.cityID INNER JOIN states AS province ON student_address.RezState = states.statesID INNER JOIN districts AS commune ON student_address.RezDistrict = districts.districtsID INNER JOIN countries AS pays ON student_address.RezCountry = countries.countryID

hrysbysz

hrysbysz1#

你应该避免这么大的结果集。有几种方法可以做到这一点。
选项1:选择所需的最小列数。
选项2:使用分页在结果集中获得有限的行数

k4aesqcs

k4aesqcs2#

使用左连接并遵循自上而下的方法,例如。
学生桌

•   studentsID  Int Pkey
  •   Fname   string
  •   Lname   string …    
  •   studentsGender  Int Fkey
  •   studentsGender  int Fkey
  •   studentsRezAddressID    int Fkey
  •   studentsBirthID int Fkey
  •   studentsParentsID   int Fkey

在上表中,首先选择all fkey并检查数据是否为require,如果是require,则连接,否则离开,如果您遵循此策略,则可能会得到结果。

cotxawn7

cotxawn73#

从一个基本查询开始,比如下一个查询,然后在每个步骤中使用join子句从另一个表中添加信息,例如:

SELECT
    students.*,
    student_address.*
FROM
    students
LEFT JOIN
    student_address ON student_address.RezAddressID = students.studentsRezAddressID
WHERE
    students.studentsID = 12;

一直这样,直到出现错误或查询挂起。另外,既然studentsid是整数列,为什么要将studentsid列与where子句上的字符串进行比较?
如果这个基本查询工作,那么我会帮助你逐步包括更多的信息,只要告诉我。。。

相关问题