简单连接查询在localhost中工作但不在服务器中工作?

时间:2010-12-19 17:10:27

标签: mysql join mysql-error-1054

以下查询在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', ''

1 个答案:

答案 0 :(得分: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'

修正,错过了第7行末尾的c.id和

为您提供更多信息: 抛出这些错误是因为您在表中引用了尚未连接且具有别名的字段。

回到原始查询:

    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'

享受,