MYSQL-如何查询/修改最大日期的那条记录

参考资料:http://stackoverflow.com/questions/6898935/sql-update-query-with-group-by-clause

--  更新数据
UPDATE product_info AS t 
INNER JOIN 
(SELECT product_id,max(update_date) update_date FROM product_info WHERE product_id = 830 GROUP BY product_id) t1 
ON t.product_id = t1.product_id AND t.update_date = t1.update_date 
SET isActive = 1,remark = 'update操作直接再上架---3!';
-- 查询最新记录
select * from product_info AS t 
INNER JOIN 
(SELECT product_id,max(update_date) update_date FROM product_info GROUP BY product_id) t1 
where t.product_id = t1.product_id AND t.update_date = t1.update_date order by t.product_id;
-- 所用表的结构
CREATE TABLE `product_info` (
    `product_id` INT(10) UNSIGNED NOT NULL,
    `product_name` VARCHAR(50) NOT NULL,
    `cost_price` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00',
    `current_price` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00',
    `update_date` DATETIME NOT NULL DEFAULT '2015-01-01 00:00:00',
    `Remark` VARCHAR(255) NOT NULL DEFAULT '',
    `isActive` TINYINT(1) NOT NULL DEFAULT '1',
    INDEX `Idx_id` (`product_id`)
)
COMMENT='商品信息表(isActive表示现有产品是否在出售)\r\n此表在新增商品的时候在stock表中添加一条记录'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

 

posted @ 2015-05-11 16:30  SchrodingerCat  阅读(1415)  评论(0编辑  收藏  举报