`where`子句中的未知列

时间:2012-10-13 22:09:16

标签: php mysql

我收到标题中描述的错误:

Unknown column 'FeedbackType' in 'where clause'

但我不明白为什么。这是我的问题:

SELECT SQL_CALC_FOUND_ROWS `Appointments`.ID, FeedbackType, FeedbackSubType 
FROM `UserFeedback`
INNER JOIN `Appointments` ON `Appointments`.ID = `UserFeedback`.Appointments_ID 
INNER JOIN `Reasons` ON `UserFeedback`.FeedbackSubType = `Reasons`.ID  
WHERE `FeedbackType` = 1  ORDER BY `Appointments`.ID ASC
LIMIT 0, 10

FeedbackTypeUserFeedback表中的一列,外壳是正确的,已经多次检查过。

为了完整性,这是表模式:

CREATE TABLE IF NOT EXISTS `UserFeedback` 
(
   ID bigint(20) NOT NULL AUTO_INCREMENT,
   FeedbackType int(4) NOT NULL,
   FeedbackSubType int(4) NOT NULL,
   Notes varchar(170) NULL,
   Appointments_ID bigint(20) NOT NULL,
   IpTracking_ID bigint(20) NOT NULL,
   PRIMARY KEY (ID),
   FOREIGN KEY (Appointments_ID) REFERENCES Appointments(Id), 
   FOREIGN KEY (IpTracking_ID) REFERENCES IpTracking(Id)    
)
ENGINE=MyISAM DEFAULT CHARSET=utf8;

可能是什么问题?

[编辑]

这些变体也不起作用(因为FeedbackType不包含保留字/字符,只属于UserFeedback表):

... WHERE UserFeedback.FeedbackType = 1
... WHERE `UserFeedback`.`FeedbackType` = 1
... WHERE FeedbackType = '1'
etc.

(我实际上看不出他们应该这么做的原因)

[编辑2]

我运行SELECT * FROM UserFeedback以确保它确实包含该列,并且我有几行,所有行都包含列(好,INSERT无误地运行。)

对于每个提到的变体,我总是会在WHERE子句中得到相同的错误。如果我省略了WHERE条款,我会得到未经过滤的结果(包括这些结果中的FeedbackType列),因此它确实令人困惑。

[解决]

出于某种原因,将WHERE查询替换为INNER JOIN内的条件将其修复为@MarinSagovac suggested in his second snippet

SELECT SQL_CALC_FOUND_ROWS `Appointments`.ID, FeedbackType, FeedbackSubType 
FROM `Appointments`
INNER JOIN `UserFeedback` ON `Appointments`.ID = `UserFeedback`.Appointments_ID 
   AND `UserFeedback`.FeedbackType = 1
INNER JOIN `Reasons` ON `UserFeedback`.FeedbackSubType = `Reasons`.ID  
ORDER BY `Appointments`.ID ASC
LIMIT 0, 10

请注意,现在没有WHERE子句,但语义应该是相同的,对吧?并且很明显该列确实存在,因此错误消息有点误导恕我直言。

5 个答案:

答案 0 :(得分:2)

你在where子句中尝试过UserFeedback.FeedbackType吗?

答案 1 :(得分:1)

尝试添加反引号:

SELECT SQL_CALC_FOUND_ROWS `Appointments`.ID, `FeedbackType`, `FeedbackSubType`
FROM `UserFeedback`
INNER JOIN `Appointments` ON `Appointments`.ID = `UserFeedback`.Appointments_ID 
INNER JOIN `Reasons` ON `UserFeedback`.FeedbackSubType = `Reasons`.ID  
WHERE `FeedbackType` = 1  ORDER BY `Appointments`.ID ASC
LIMIT 0, 10

添加了尝试:

SELECT SQL_CALC_FOUND_ROWS `Appointments`.ID, FeedbackType, FeedbackSubType 
FROM `UserFeedback`
INNER JOIN `Appointments` ON `Appointments`.ID = `UserFeedback`.Appointments_ID 
INNER JOIN `Reasons` ON `UserFeedback`.FeedbackSubType = `Reasons`.ID  
INNER JOIN `UserFeedback`.`FeedbackType` = 1  ORDER BY `Appointments`.ID ASC
LIMIT 0, 10

答案 2 :(得分:0)

SELECT SQL_CALC_FOUND_ROWS `Appointments`.ID, UserFeedback.FeedbackType, `UserFeedback.FeedbackSubType 
FROM `UserFeedback'
INNER JOIN `Appointments` ON `Appointments`.ID = `UserFeedback`.Appointments_ID 
INNER JOIN `Reasons` ON `UserFeedback`.FeedbackSubType = `Reasons`.ID  
WHERE UserFeedback.FeedbackType = 1  ORDER BY `Appointments`.ID ASC
LIMIT 0, 10

立即尝试

答案 3 :(得分:0)

使用myisam时的外键?

这可能就是问题

答案 4 :(得分:-2)

在WHERE子句中尝试:

WHERE UserFeedback.FeedbackType ='1'

删除字段名称中的所有引号(')