SQL查询问题
我有以下查询失败,我不明白为什么。显然,代码所抱怨的列在表中。
查询:
INSERT
INTO `ProductAudit` (`Id`, `Field`, `OldValue`, `NewValue`, `ChangedOn`)
SELECT t.`ProductId`, 'Title', p.`Title`, t.`Title`, t.`ProcessedOn`
FROM `TempImport` t
LEFT JOIN `Product` p
ON t.`ProductId` = p.`Id`
WHERE p.`Title` != t.`Title`
ON DUPLICATE KEY UPDATE
p.`ChangedOn` = VALUES(`ChangedOn`)
表:
CREATE TABLE `ProductAudit` (
`Id` varchar(32) NOT NULL,
`Field` varchar(16) NOT NULL,
`OldValue` text,
`NewValue` text,
`ChangedOn` date NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
错误:
Error Code: 1054 - Unknown column 'p.ChangedOn' in 'field list'
I have the following query that is failing and I don't understand why. Clearly the column that the code is complaining about is in the table.
Query:
INSERT
INTO `ProductAudit` (`Id`, `Field`, `OldValue`, `NewValue`, `ChangedOn`)
SELECT t.`ProductId`, 'Title', p.`Title`, t.`Title`, t.`ProcessedOn`
FROM `TempImport` t
LEFT JOIN `Product` p
ON t.`ProductId` = p.`Id`
WHERE p.`Title` != t.`Title`
ON DUPLICATE KEY UPDATE
p.`ChangedOn` = VALUES(`ChangedOn`)
Table:
CREATE TABLE `ProductAudit` (
`Id` varchar(32) NOT NULL,
`Field` varchar(16) NOT NULL,
`OldValue` text,
`NewValue` text,
`ChangedOn` date NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Error:
Error Code: 1054 - Unknown column 'p.ChangedOn' in 'field list'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您为其列出 CREATE 语句的表与您在查询中引用的表不同。 P 是 Product 表,而不是 ProductAudit 表。
The table you list the CREATE statement for is not the same table you are referencing in the query. P is the Product table, not the ProductAudit table.
在
ON DUPLICATE KEY UPDATE
短语中,您只能更新您尝试插入的表。In the
ON DUPLICATE KEY UPDATE
phrase, you can only update the table which you are trying to insert into.