2010-12-19 66 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

相关问题