我收到标题中描述的错误:
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
FeedbackType
是表中的一列UserFeedback
,大小写正确,已经检查了几次。
为了完整起见,这是表模式:
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
s 工作没有错误)。
对于提到的每个变体,我总是在WHERE
子句中得到相同的错误。如果我省略该WHERE
子句,我会得到未经过滤的结果(包括FeedbackType
这些结果中的列),所以这真的很混乱。
[解决方案]
出于某种原因,正如@MarinSagovac在他的第二个片段中建议的WHERE
那样,将查询替换为内部的条件已修复:INNER JOIN
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
子句,但语义应该是一样的,对吧?很明显,该列确实存在,因此错误消息有点误导恕我直言。