MySQL / SQL查询多个表

时间:2018-09-28 19:01:06

标签: mysql sql

我有多个表格,我正在尝试通过ID“ studentID”搜索学生。当我在下面运行查询时,MySQL或SQL Server冻结(正在加载...)。有什么建议吗?并非所有表格都列出。谢谢。

表格:

学生

•   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

student_edu:

•   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

3 个答案:

答案 0 :(得分:0)

您应避免设置这么大的结果集。有几种方法可以做到这一点。

选项1:选择所需的最小列数。

选项2:使用分页获取结果集中有限的行数

答案 1 :(得分:0)

从基础查询开始,例如下一个查询,并在每个步骤上使用JOIN子句从另一个表中添加信息,例如:

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

以这种方式进行操作,直到出现错误或查询挂起。另外,既然学生ID是整数列,为什么还要在WHERE子句上将studentsID列与字符串进行比较?

如果此基本查询有效,那么我将帮助您逐步包括更多信息,只是告诉我...

答案 2 :(得分:0)

继续左联接,并遵循自上而下的方法,例如。

学生桌

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

在上表中,首先,选择所有Fkey并检查是否需要数据,然后加入,否则将其保留,如果遵循此策略,则可能会得到结果。