【问题标题】:INNER JOIN issue内部连接问题
【发布时间】:2011-02-23 21:55:50
【问题描述】:

我从这个查询中得到以下错误:

1054 - “on 子句”中的未知列“sheet_items.color_id”

select sheet_items.sheet_id, sheet_items.quantity, materials.material, colors.color
from sheet_items, materials
inner join colors
on colors.color_id=sheet_items.color_id
where sheet_items.sheet_id=4 and sheet_items.material_id=materials.material_id

有什么想法吗?提前致谢!

【问题讨论】:

  • 好吧,sheet_items 有一个名为 color_id 的列吗?
  • 你能粘贴这些表的架构吗?消息很清楚,您在表 sheet_items 中没有名为 color_id 的列
  • 我确实有一个名为 color_id 的列,这就是为什么它是一个奇怪的错误。

标签: mysql mysql-error-1054


【解决方案1】:

按照您现在的结构方式,您正在尝试将材质与颜色进行内部连接,因此,它不知道您在说什么列.. 尝试

select sheet_items.sheet_id, sheet_items.quantity, materials.material, colors.color
from materials,sheet_items
inner join colors
on colors.color_id=sheet_items.color_id
where sheet_items.sheet_id=4 and sheet_items.material_id=materials.material_id

【讨论】:

  • 如果不存在则创建表 sheet_items ( sheet_item_id int(11) NOT NULL AUTO_INCREMENT, sheet_id int(11) NOT NULL, user_id int(11) NOT NULL, @987654326 @ int(11) NOT NULL, color_id int(11) DEFAULT NULL, quantity int(11) NOT NULL, timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (sheet_item_id) ) ENGINE= InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
【解决方案2】:

看起来您在 ANSI92 SQL 前后混合,请尝试...

PRE ANSI92

select sheet_items.sheet_id, sheet_items.quantity, materials.material, colors.color
from sheet_items, materials, colors
where sheet_items.sheet_id=4 and sheet_items.material_id=materials.material_id AND
colors.color_id=sheet_items.color_id

发布 ANSI92

select sheet_items.sheet_id, sheet_items.quantity, materials.material, colors.color
from sheet_items
inner join materials
on sheet_items.material_id=materials.material_id
inner join colors
on colors.color_id=sheet_items.color_id
where sheet_items.sheet_id=4

无论哪种方式恕我直言,第二种格式更易于阅读/理解和调试,并且适用于所有 SQL 平台

【讨论】:

    【解决方案3】:

    在 sheet_items 和 material 之间的 where 子句中有一个隐式的内部连接。你应该把它从你的 where 子句中取出并放入你的投影中,这样它看起来像这样:

    select sheet_items.sheet_id, sheet_items.quantity, materials.material, colors.color from materials
    inner join sheet_items on materials.material_id=sheet_items.material_id 
    inner join colors on colors.color_id=sheet_items.color_id 
    where sheet_items.sheet_id=4
    

    【讨论】:

      【解决方案4】:

      不要混淆你做 JOIN 的方式,它会很混乱。

      SELECT sheet_items.sheet_id, sheet_items.quantity, materials.material, colors.color
      FROM
      sheet_items
      INNER JOIN materials ON materials.id=sheet_items.material_id
      INNER JOIN colors ON colors.color_id=sheet_items.color_id
      WHERE
      sheet_items.sheet_id=4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-07
        相关资源
        最近更新 更多