简体   繁体   中英

Unknown column in JOIN many tables

can u guys help me fix this up..

the error said ' Unknown column 'd.idspesialis' in 'on clause' '

SELECT a.*, b.*, c.*,  e.*,d.*, f.*,g.*
FROM ckehamilan a 
    INNER JOIN  pasien  b 
            ON a.noRM=b.noRM 
    INNER JOIN ( select idPegawai , NamaPegawai as NamaDokter from tpegawai )  d 
            ON a.idPegawai=d.idPegawai 
     LEFT JOIN spesialis e 
            ON e.idspesialis=d.idspesialis 
    INNER JOIN ( select idPegawai , NamaPegawai as NamaParamedis from  tpegawai )  f 
            ON a.idparamedis=f.idPegawai 
     LEFT JOIN imunisasi g 
            ON a.idimunisasi=g.idimunisasi
    INNER JOIN  ruang  c 
            ON a.idruang=c.idruang 

your subquery doesn't have column idspesialis

try this,

(
    SELECT idPegawai, NamaPegawai AS NamaDokter, idspesialis
    FROM tpegawai
) d

your full query should look like below,

SELECT a.*
    , b.*
    , c.*
    , e.*
    , d.*
    , f.*
    , g.*
FROM ckehamilan a
    INNER JOIN pasien b
        ON a.noRM = b.noRM
    INNER JOIN 
        (
            SELECT idPegawai, NamaPegawai AS NamaDokter, idspesialis
            FROM tpegawai
        ) d
        ON a.idPegawai = d.idPegawai
    LEFT JOIN spesialis e
        ON e.idspesialis = d.idspesialis
    INNER JOIN 
        (
            SELECT idPegawai , NamaPegawai AS NamaParamedis
            FROM tpegawai
        ) f
        ON a.idparamedis = f.idPegawai
    LEFT JOIN imunisasi g
        ON a.idimunisasi = g.idimunisasi
    INNER JOIN ruang c
        ON a.idruang = c.idruang

In your table with alias d , you do not have a column named d.idspesialis and hence it is throwing the error.

Your d table is as below -

( select idPegawai , NamaPegawai as NamaDokter from tpegawai )  d 

and you are trying to add join with e as

e.idspesialis=d.idspesialis 

You have to add that column in Inner select query to use in parent query. Check below query:

INNER JOIN ( SELECT idPegawai , NamaPegawai AS NamaDokter, idspesialis  FROM tpegawai )  d ON a.idPegawai=d.idPegawai 

Full Query:

SELECT a.*, b.*, c.*,  e.*,d.*, f.*,g.* 
FROM ckehamilan a 
INNER JOIN  pasien  b ON a.noRM=b.noRM 
INNER JOIN ( SELECT idPegawai , NamaPegawai AS NamaDokter, idspesialis  FROM tpegawai )  d ON a.idPegawai=d.idPegawai 
LEFT JOIN spesialis e ON e.idspesialis=d.idspesialis 
INNER JOIN ( SELECT idPegawai , NamaPegawai AS NamaParamedis FROM  tpegawai )  f ON a.idparamedis=f.idPegawai 
LEFT JOIN imunisasi g ON a.idimunisasi=g.idimunisasi
INNER JOIN  ruang  c ON a.idruang=c.idruang 

Try this ::

 select a.*, b.*, c.*,  e.*,d.idPegawai, d.NamaPegawai as NamaDokter, g.*, f.*
  from ckehamilan a 
   INNER JOIN  pasien  b ON a.noRM=b.noRM 
   INNER JOIN tpegawai  d ON a.idPegawai=d.idPegawai 
   LEFT JOIN spesialis e ON e.idspesialis=d.idspesialis 
   LEFT JOIN imunisasi g ON a.idimunisasi=g.idimunisasi
INNER JOIN tpegawai f ON a.idparamedis = f.idPegawai
    INNER JOIN  ruang  c ON a.idruang=c.idruang 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM