【问题标题】:simple join query working in localhost but not in server?简单的连接查询在本地主机中工作但在服务器中不工作?
【发布时间】:2010-12-19 17:10:26
【问题描述】:

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

【问题讨论】:

  • "join holiday as h on hd.holiday_id=hc.id" 仔细查看这一行。
  • 在localhost上运行正常,本地mysql版本为4.1.10,服务器mysql版本为5.1.52

标签: mysql join mysql-error-1054


【解决方案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,但在第 7 行加入并创建别名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'

享受,

【讨论】:

  • 现在它在您的查询的“on 子句”错误中显示未知列“c.id”
  • 我更新了我的答案以解决您提出的问题并为您添加更多信息。
  • 感谢您对参考的详细解释,但该查询几乎可以正常工作,但数据集错误。输出中的日期错误。加入出现问题
  • 我看不出查询有什么问题。请通过一些示例行发布有关您的表结构的更多详细信息,以便我们更好地为您提供帮助。
  • 在我的回答中再试一次。
猜你喜欢
  • 2014-06-26
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2014-01-15
相关资源
最近更新 更多