MySQL错误代码1166.列名不正确

时间:2012-03-28 20:15:41

标签: mysql sql

当我尝试运行例程时,我收到以下错误:

  

错误代码:1166。错误的列名称'学校。学校姓名`=案例   当学校.Web学校名称`为null然后学校。学校名字`   ELS'

我仔细检查了列名,实际上有一个School.School Name。没有前导或尾随空格。这是错误所指的例程的开始:

CREATE temporary TABLE tt_Step1
SELECT DISTINCT State.Code, State.Name, 
School.`School Name` = case 
    when School.`Web School Name` is null then School.`School Name`
    else School.`Web School Name`
    end,
School.`School ID` 
-- Into tt_Step1
FROM State LEFT JOIN School ON State.Code = School.State 
Where (School.`School ID` <> ...

我最近将此代码从MSSQL转换为MySQL,因此我可能会遗漏一些但我找不到任何错误。原始的MSSQL查询在SQL Server Management Studio中运行良好,但在MySQL中转换后的版本却没有。

4 个答案:

答案 0 :(得分:4)

试一试:

CREATE temporary TABLE tt_Step1
SELECT DISTINCT State.Code, State.Name, 
coalesce(School.`Web School Name`, School.`School Name`),
School.`School ID` 
-- Into tt_Step1
FROM State LEFT JOIN School ON State.Code = School.State 
Where (School.`School ID` <> ...

答案 1 :(得分:2)

你有几个问题。

  1. 您不能使用多部分名称(例如School.School Name
  2. 为列添加别名
  3. MySql中的别名由blah as ColumnName完成,而不是ColumnName = blah

答案 2 :(得分:2)

In my case, it was caused by an unseen extra space after the column name. I wrote promotion-ids instead of promotion-id.

答案 3 :(得分:-1)

我有类似的问题。通过在case语句中添加列名来解决我的问题。

示例:

Case

  when val <.5 then "low"

  when val >1 then "high"

end as valTest

如果没有as valTest,我会收到1166错误。