开发者

simple join query working in localhost but not in server?

The following query runs well on localhost and returns rows but when it is executed in server it returns error...

The error shown is

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

What the issue?

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'

My table structure is countries

'id', 'int(11)', '', 'PRI', '', 'auto_increment开发者_JAVA百科'
'name', 'varchar(80)', 'YES', '', '', ''

holidays

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


The order you have your joins is messed up, it looks like it was a typo at the end of my 6th line

    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'

Fixed, missed the and c.id at the end of line 7

A little more information for you: These errors are being thrown because you are referencing fields in a table that have not yet been joined and had an alias made.

so back to your original query:

    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'

Your original error was because you reference the table hc on line 6, but join it and make an alias at line 7. The second error was because you reference the table c on line 7, but join and make an alias on line 8.

Edit:

This doesn't make much logical sense to me without the table structure, but try this one:

   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'

Enjoy,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜