2010-12-19 33 views
0

下面的查詢以及運行在本地主機上,並返回行,但是當它在服務器中執行它返回錯誤......在localhost中工作但不在服務器中的簡單連接查詢?

所示的錯誤是

#1054 - Unknown column 'hc.id' in 'on clause' 

什麼問題?

select 
hd.holiday_id,h.same_date, h.holiday, hd.date 
from holiday_dates as hd 
join holidays as h on hd.holiday_id=hc.id 
join holiday_countries as hc on hc.holiday_id=h.id and hc.country_id=c.id 
join countries as c 
where 
c.name='india' and hd.year='2010' 

我的表結構 國家

'id', 'int(11)', '', 'PRI', '', 'auto_increment' 
'name', 'varchar(80)', 'YES', '', '', '' 

假期

'id', 'int(11)', '', 'PRI', '', 'auto_increment' 
'holiday', 'varchar(90)', 'YES', '', '', '' 
'same_date', 'tinyint(1)', 'YES', '', '', '' 
'religions', 'varchar(50)', '', '', '', '' 
'season', 'enum('Winter','Spring','Summer','Autumn')', '', '', 'Winter', '' 
'rate', 'int(2)', '', '', '0', '' 

holiday_countries

'id', 'int(11)', '', 'PRI', '', 'auto_increment' 
'holiday_id', 'int(11)', '', '', '0', '' 
'country_id', 'int(11)', '', '', '0', '' 
'link', 'varchar(40)', '', '', '', '' 

holiday_dates

'holiday_id', 'int(11)', 'YES', 'MUL', '', '' // this refers to the holiday_id from holiday_countries table 
'year', 'varchar(4)', 'YES', '', '', '' 
'date', 'date', '', '', '0000-00-00', '' 
+0

「加盟假期作爲hd.holiday_id = hc.id H」 仔細看這條線。 – 2010-12-19 17:14:36

+0

它在localhost上運行正常,本地mysql版本是4.1.10,服務器mysql版本是5.1.52 – Clewon 2010-12-19 17:22:35

回答

1

你有你的加入被搞砸的順序,它看起來就像是我的6號線

select hd.holiday_id 
     , h.same_date 
     , h.holiday 
     , hd.date 
     from holiday_dates as hd 
     join holidays as h on hd.holiday_id = h.id 
     join holiday_countries as hc on hc.holiday_id = h.id 
     join countries as c on hc.country_id = c.id 
    where c.name='india' 
     and hd.year='2010' 

修正結束一個錯字,錯過了和c.id在行7末尾

更多信息給你: 這些錯誤正在拋出becau如果您正在引用表中還沒有加入並且有別名的字段。

做回原來的查詢:

select hd.holiday_id 
     , h.same_date 
     , h.holiday 
     , hd.date 
     from holiday_dates as hd 
     join holidays as h on hd.holiday_id=hc.id 
     join holiday_countries as hc on hc.holiday_id=h.id and hc.country_id=c.id 
     join countries as c 
    where c.name='india' 
     and hd.year='2010' 

你原來的錯誤是因爲你參考第6行的表HC,但加入它,使一個別名在第7行的第二個錯誤是因爲你參考第7行的表C,但加入並就行別名8.

編輯:

這並沒有太大的邏輯意義對我來說沒有表結構,但試試這個:

select hd.holiday_id 
     , h.same_date 
     , h.holiday 
     , hd.date 
     from holidays as h 
     join holiday_countries as hc on hc.holiday_id = h.id 
     join holiday_dates as hd on hd.holiday_id = hc.id 
     join countries as c on hc.country_id = c.id 
    where c.name='india' 
     and hd.year='2010' 

享受,

+0

現在它在'on clause'錯誤中顯示未知列'c.id' – Clewon 2010-12-19 17:24:47

+0

我更新了我的答案您提出的問題併爲您添加更多信息。 – 2010-12-19 17:56:58

+0

感謝您對參考文獻的精細解釋,但該查詢幾乎可以正常工作,但出現了一組錯誤的數據..日期在輸出中是錯誤的。加入 – Clewon 2010-12-19 18:28:58

相關問題