SQL查询Phpmyadmin没有给出正确的结果,有3个以上的表与WHERE子句

lnlaulya  于 2022-12-18  发布在  PHP
关注(0)|答案(1)|浏览(104)

我有关系数据库,并试图执行以下查询多达3个表它是给我正确的结果,即248记录的学生在学院id 910,但当我试图使一个查询有超过3个表它给我19000+结果.

SELECT * 
FROM Student,StudentRegistration,RefStudentType,RefGender,SubjectCategory 
WHERE Student.student_id=StudentRegistration.student_student_id 
AND StudentRegistration.reg_student_type_std_type_id = RefStudentType.std_type_id 
AND Student.student_gender_gender_id = RefGender.gender_id 
AND StudentRegistration.reg_student_subjectCat_sub_cat_id=SubjectCategory.sub_cat_id 
AND Student.student_institute_inst_id=910;

'
也尝试使用JOIN,但相同的19000+记录了错误结果
SELECT * FROM Student INNER JOIN StudentRegistration ON student_id=student_student_id INNER JOIN RefReligion ON RefReligion.religion_id=Student.student_religion_religion_id INNER JOIN RefStudentType ON RefStudentType.std_type_id=StudentRegistration.reg_student_type_std_type_id WHERE student_institute_inst_id=910;
任何解决方案、查询逻辑错误或新内容

vfh0ocws

vfh0ocws1#

我认为这是由于一个数据有多个记录。例如,在SubjectCategory表中可能有多个id = '910'的记录
最好使用左/右/内/外连接,而不要使用from tbl 1、tbl 2
我的建议是用身份证一张一张地查一下table。

相关问题