如何解决SQL中的LEFT JOIN?

时间:2017-03-13 03:14:02

标签: mysql sql

我这样查询:

select a.NIM
     , k.IPK
     , m.nama Mayor
  from kelanjutanstudi k
  left 
  join mahasiswamagister a
    on k.ID =a.MahasiswaID
  left 
  join mayor m
    on m.ID = a.MayorID

当我试图运行它时,结果并不像我预期的那样。因为结果是这样的

enter image description here

实际上,NIM和Mayor有自己的价值,但是当我运行查询时,NIM和Mayor的值为NULL。也许,我的sql代码有问题。这是我的表:

mahasiswamagister表

enter image description here

kelanjutanstudi表

enter image description here

市长表 enter image description here

请帮我解决这个问题。三江源

1 个答案:

答案 0 :(得分:0)

left join更改为Inner Join:

select 
`mahasiswamagister`.`NIM` AS `NIM`,
`kelanjutanstudi`.`IPK` AS `IPK`,
`mayor`.`Nama` AS `Mayor`
from ((`kelanjutanstudi` 
inner join 
`mahasiswamagister`
 on((`kelanjutanstudi`.`ID` = `mahasiswamagister`.`MahasiswaID`)))
inner join
 `mayor` 
on((`mayor`.`ID` = `mahasiswamagister`.`MayorID`)))