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', ''

假日国家

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

假期日期

'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', ''
4

1 回答 1

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'

享受,

于 2010-12-19T17:15:24.877 回答