我在mysql程序中有这一行
UPDATE mytable SET product_code = p_code WHERE mytable.product_id=p_id ;
它产生了这个错误:
Error Code: 1054. Unknown column product_code in 'field list'
我不明白这是什么问题?为什么会出现这个错误?
更新: 表结构如下:
CREATE TABLE `mytable` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`password` varchar(100) NOT NULL,
`product_id` int(11) NOT NULL,
`another_id` int(11) NOT NULL DEFAULT '1',
`forein_key` int(11) NOT NULL DEFAULT '2',
`create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`some_other_field` int(11) NOT NULL DEFAULT '0',
`other_field` datetime DEFAULT NULL,
`product_code` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`,`another_id`,`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3553 DEFAULT CHARSET=latin1;
步骤:
DROP PROCEDURE IF EXISTS UPDATE_PRODUCT_ID $$
CREATE PROCEDURE UPDATE_PRODUCT_ID()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE p_code varchar(20);
DECLARE p_id int(11);
DECLARE cur1 CURSOR FOR SELECT product_id, product_code FROM product;
SET SQL_SAFE_UPDATES = 0;
SELECT COUNT(*) FROM product INTO n;
SET i=0;
OPEN cur1;
WHILE i<n DO
FETCH cur1 INTO p_id, p_code;
UPDATE mytable SET mytable.product_code = p_code WHERE mytable.product_id=p_id ;
SET i = i + 1;
END WHILE;
SET SQL_SAFE_UPDATES = 1;
End $$
DELIMITER ;
CALL UPDATE_PRODUCT_ID();