Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query SQL Server for JOIN and WHERE

I have problem with my sql query.
I want to join 2 table, with condition using where ‘id_pegawai’ and ‘status’ = 1.

Table1: SELECT * FROM form_pertambahan_anak
enter image description here

Table2: SELECT * FROM peg_dtl_anak WHERE id_pegawai = '0000000360'
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My Query: SELECT fpa.id_pegawai, fpa.tanggal_pengusulan, fpa.id_anak, pda.id, pda.nama_anak, fpa.deleted_at, fpa.status FROM form_pertambahan_anak fpa INNER JOIN peg_dtl_anak pda ON pda.id_pegawai = fpa.id_pegawai WHERE fpa.id_pegawai = '0000000360' AND fpa.deleted_at IS NULL AND fpa.status = '1'

enter image description here

Why return 2 data? 1 expect just showing 1 data from form_pertambahan_anak with status = 1.

>Solution :

As far as I can see, you need to add an id_anak equality condition to the query:

SELECT fpa.id_pegawai
     , fpa.tanggal_pengusulan
     , fpa.id_anak
     , pda.id
     , pda.nama_anak
     , fpa.deleted_at
     , fpa.status
  FROM form_pertambahan_anak fpa
  INNER JOIN peg_dtl_anak pda ON pda.id_pegawai = fpa.id_pegawai
                             AND pda.id = fpa.id_anak --here
WHERE fpa.id_pegawai = '0000000360'
  AND fpa.deleted_at IS NULL
  AND fpa.status = '1'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading