我的查询是否有任何错误的代码加入表格?

时间:2017-03-11 06:50:49

标签: mysql sql

我对此代码感到困惑。我有这样的查询

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `aIPK` AS select 
`ipbmst_fakultas`.`Kode` AS `Fakultas`,
`ipbmst_departemen`.`Kode` AS `Departemen`,
`akdmst_mahasiswamagister`.`NIM` AS `NIM`,
`akdmst_mahasiswamagister`.`TahunMasuk` AS `TahunMasuk`,
 `akdhis_kelanjutanstudi`.`IPK` AS `IPK`
from (((((`akdmst_mahasiswamagister` left join `akdmst_mayor` on((`akdmst_mahasiswamagister`.`MayorID` = `akdmst_mayor`.`ID`))) 
left join `ipbmst_departemen` on((`akdmst_mayor`.`DepartemenID` = `ipbmst_departemen`.`ID`))) 
left join `ipbmst_fakultas` on((`ipbmst_departemen`.`FakultasID` = `ipbmst_fakultas`.`ID`))) 
left join `ipbmst_orang` on((`akdmst_mahasiswamagister`.`NIM` = `ipbmst_orang`.`NIMS2Key`))) 
left join `akdhis_kelanjutanstudi` on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL

order by NIM
LIMIT 100;

但" IPK"结果为NULL,实际上IPK有其值。这些代码有什么问题?

这是我的表结构:

table 1: akdmst_mahasiswamagister (ID, MahasiswaID, NIM, MayorID, TahunMasuk)
table 2: akdmst_mayor(ID, DepartemenID)
table 3: ipbmst_departemen(ID, FakultasID, DepartmenName)
table 4: ipbmst_fakultas(ID, FacultyName)
table 5: ipbmst_orang(ID, Name, NIMS2Key)
table 6: akdhis_kelanjutanstudi(ID, NIM, IPK)

我还有关于此查询的其他问题。查询视图花了太多时间。我想也许是因为表akdhis_kelanjutanstudi包含超过300K的数据记录。我用了#34; LIMIT 100"但仍然是一样的。你能帮我解决一下这个问题吗?

3 个答案:

答案 0 :(得分:2)

我不知道确切的问题是什么,但您的WHERE条款存在问题:

WHERE IPK IS NOT NULL

不允许在WHERE子句中引用列别名,因为其值可能尚未确定。相反,你应该使用它:

WHERE akdhis_kelanjutanstudi.IPK IS NOT NULL

<强>更新

您在原始视图中使用的括号看起来很奇怪,不必要,也可能是错误的。请尝试使用以下内容:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost`
    SQL SECURITY DEFINER VIEW aIPK AS
SELECT t4.Kode AS Fakultas,
       t3.Kode AS Departemen,
       t1.NIM AS NIM,
       t1.TahunMasuk AS TahunMasuk,
       t6.IPK AS IPK
FROM akdmst_mahasiswamagister t1
LEFT JOIN akdmst_mayor t2
    ON t1.MayorID = t2.ID
LEFT JOIN ipbmst_departemen t3
    ON t2.DepartemenID = t3.ID
LEFT JOIN ipbmst_fakultason t4
    ON t3.FakultasID = t4.ID
LEFT JOIN ipbmst_orang t5
    ON t1.NIM = t5.NIMS2Key
LEFT JOIN akdhis_kelanjutanstudi t6
    ON t6.NIM = t5.NIMS2Key
WHERE t6.IPK IS NOT NULL
ORDER BY NIM
LIMIT 100;

答案 1 :(得分:0)

运行sql而不是创建算法更容易进行测试。

在您的情况下,检查加入条件

on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))

你也需要改变

WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL

WHERE `IPK` IS NOT NULL

答案 2 :(得分:0)

你的描述中有错误,

你的桌子是

表1:akdhis_mahasiswamagister(ID,MahasiswaID,NIM,MayorID,TahunMasuk) 表2:akdmst_mayor(ID,DepartemenID) 表3:ipbmst_departemen(ID,FakultasID,DepartmenName) 表4:ipbmst_fakultas(ID,FacultyName) 表5:ipbmst_orang(ID,Name,NIMS2Key) 表6:akdhis_kelanjutanstudi(ID,NIM,IPK)

但我在选择

中看不到“akdhis_mahasiswamagister”表

CREATE ALGORITHM = UNDEFINED DEFINER = root @ localhost SQL安全定义视图aIPK AS选择 ipbmst_fakultasKode AS Fakultasipbmst_departemenKode AS Departemenakdmst_mahasiswamagisterNIM AS NIMakdmst_mahasiswamagisterTahunMasuk AS TahunMasuk,  akdhis_kelanjutanstudiIPK AS IPK from(((({akdmst_mahasiswamagister内部联接akdmst_mayor on(akdmst_mahasiswamagisterMayorID = akdmst_mayorID))) 内部联接ipbmst_departemen on((akdmst_mayorDepartemenID = ipbmst_departemenID))) 内部联接ipbmst_fakultas on((ipbmst_departemenFakultasID = ipbmst_fakultasID))) 内部联接ipbmst_orang on((akdmst_mahasiswamagisterNIM = ipbmst_orangNIMS2Key))) 内部联接akdhis_kelanjutanstudi on((akdhis_kelanjutanstudiNIM = ipbmst_orangNIMS2Key))) IPK不是空的

由NIM订购 限制100;

akdhis_mahasiswamagister和akdmst_mahasiswamagister有什么不同?你可能使用了错误的表格,所以你的信息不正确