JOIN很多表中的未知列

时间:2012-12-07 06:58:10

标签: mysql sql join

你们可以帮我解决这个问题..

错误在'on clause''

中说'未知列'd.idspesialis'
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 

4 个答案:

答案 0 :(得分:1)

您的子查询没有列idspesialis

试试这个,

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

您的完整查询应如下所示,

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

答案 1 :(得分:0)

在包含别名d的表中,您没有名为d.idspesialis的列,因此它会抛出错误。

您的d表格如下 -

( select idPegawai , NamaPegawai as NamaDokter from tpegawai )  d 

并且您尝试将e添加为

e.idspesialis=d.idspesialis 

答案 2 :(得分:0)

您必须在内部选择查询中添加该列以在父查询中使用。检查以下查询:

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

完整查询:

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 

答案 3 :(得分:0)

试试这个::

 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