Mysql Join two Tables? -


i have 2 tables :

tbl_jurusan_snmptn 

column :

kode_jurusan varchar(50)   jurusan varchar(90) 

and session_ujian_snmptn

column :

   id_ujian    pilihan_1  varchar(50)    pilihan_2  varchar(50)    pilihan_3  varchar(50) 

in tbl_jurusan_snmptn have values:

kode_jurusan    jurusan  10221           teknik informatika  10223           kedokteran  10999           hukum  11999           geologi 

session_ujian_snmptn values :

    id_ujian   pilihan_1    pilihan_2    pilihan_3         001        10223        11999        10999       002        11999        10221        10999 

i've tried query :

 select * session_ujian_snmptn   inner join `tbl_jurusan_snmptn` b  on a.pilihan_1 = b.kode_jurusan  or a.pilihan_2 = b.kode_jurusan  or a.pilihan_3 = b.kode_jurusan  a.id_ujian = '001' 

but show result this:

id_ujian   pilihan_1    pilihan_2    pilihan_3   kode_jurusan    jurusan     001        10223        11999        10999      10221           kedokteran    001        10223        11999        10999      11999           geologi    001        10223        11999        10999      1099            hukum 

actually need :

  id_ujian   pilihan_1    pilihan_2    pilihan_3      001        kedokteran    geologi      hukum       

every appreciated. thanks.

try :

   select id_ujian ,    max(case when a.pilihan_1 = b.kode_jurusan jurusan end )as 'pilihan_1' ,     max(case when a.pilihan_2 = b.kode_jurusan jurusan end )as 'pilihan_2 ' ,    max(case when a.pilihan_3 = b.kode_jurusan jurusan end )as 'pilihan_3'           session_ujian_snmptn     inner join `tbl_jurusan_snmptn` b     on a.pilihan_1 = b.kode_jurusan     or a.pilihan_2 = b.kode_jurusan     or a.pilihan_3 = b.kode_jurusan     a.id_ujian = '001' 

demo here

if want select rows consider use group by

 select id_ujian ,  max(case when a.pilihan_1 = b.kode_jurusan jurusan end )as 'pilihan_1' ,   max(case when a.pilihan_2 = b.kode_jurusan jurusan end )as 'pilihan_2 ' ,  max(case when a.pilihan_3 = b.kode_jurusan jurusan end )as 'pilihan_3'         session_ujian_snmptn   inner join `tbl_jurusan_snmptn` b   on a.pilihan_1 = b.kode_jurusan   or a.pilihan_2 = b.kode_jurusan   or a.pilihan_3 = b.kode_jurusan   group id_ujian 

demo here


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -