0

I'm using Mysql and I difficult time trying to get the results from a SELECT query. I am having 2 tables. First table jam and second table jadwalblok. data in this table is static:

select * from jam
idjam nmjam
01    09.00-09.50
02    10.00-10.50
03    11.00-11.50
04    12.00-13.00   

select * from jadwalblok
idjadwal idjam ruang tgl
1        01    601   2017-04-24
2        03    602   2017-04-25

I used joins to get results as.

SELECT jam.idjam,
       jam.nmjam,
       jadwalblok.idruang,
       jadwalblok.tgl
FROM jam
LEFT JOIN jadwalblok ON jadwalblok.idjam = jam.idjam
WHERE jadwalblok.tgl='2017-04-24'

But I am not getting correct result. I want results as shown below:

idjam nmjam        ruang tgl
01    09.00-09.50  601   2017-04-24
02    10.00-10.50  null  null
03    11.00-11.50  null  null
04    12.00-13.00  null  null
1
  • Next time pls show the results you get when you run your query, that's a huge help. In this particular case I could determine what had gone wrong, but this may not be the case for a more complicated query. Commented Jan 16, 2017 at 1:35

1 Answer 1

2

You need to move the jadwalblok.tgl='2017-04-24' criterion from the where clause to the join condition because the where clause is applied after the 2 tables are joined, while the join condition is applied during the join:

SELECT jam.idjam,
       jam.nmjam,
       jadwalblok.idruang,
       jadwalblok.tgl
FROM jam
LEFT JOIN jadwalblok ON jadwalblok.idjam = jam.idjam and jadwalblok.tgl='2017-04-24'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.