Is there any wrong code with my query to join table?

后端 未结 3 891
逝去的感伤
逝去的感伤 2021-01-26 06:28

I am really confused with this codes. I have query like this

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `aIPK` AS select 
`i         


        
相关标签:
3条回答
  • 2021-01-26 06:55

    I don't know what the exact problem(s) is, but your WHERE clause has a problem:

    WHERE IPK IS NOT NULL
    

    It is not allowed to refer to a column alias in the WHERE clause, because its value may not be determined yet. Instead, you should use this:

    WHERE akdhis_kelanjutanstudi.IPK IS NOT NULL
    

    Update:

    The parentheses you used in your original view look strange, unnecessary, and possibly wrong. Try using the following:

    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;
    
    0 讨论(0)
  • 2021-01-26 07:06

    Running sql instead of creating algorithm is easier for testing.

    In your case check the conditions in join

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

    also you need to change

    WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL

    To

    WHERE `IPK` IS NOT NULL

    0 讨论(0)
  • 2021-01-26 07:13

    you have mistake in your description,

    your tables are

    table 1: akdhis_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)

    but i dont see "akdhis_mahasiswamagister" table in your select

    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 inner join akdmst_mayor on((akdmst_mahasiswamagister.MayorID = akdmst_mayor.ID))) inner join ipbmst_departemen on((akdmst_mayor.DepartemenID = ipbmst_departemen.ID))) inner join ipbmst_fakultas on((ipbmst_departemen.FakultasID = ipbmst_fakultas.ID))) inner join ipbmst_orang on((akdmst_mahasiswamagister.NIM = ipbmst_orang.NIMS2Key))) inner join akdhis_kelanjutanstudi on((akdhis_kelanjutanstudi.NIM = ipbmst_orang.NIMS2Key))) WHERE IPK IS NOT NULL

    order by NIM LIMIT 100;

    akdhis_mahasiswamagister and akdmst_mahasiswamagister are different? may you are using wrong table, and so you have incorrect information

    0 讨论(0)
提交回复
热议问题