CREATE TABLE IF NOT EXISTS `order_order_status` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`order_status_id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `order_order_status_order_status_id_index` (`order_status_id`),
KEY `order_order_status_order_id_index` (`order_id`),
KEY `order_order_status_created_at_index` (`created_at`),
KEY `order_order_status_updated_at_index` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
--
-- Dumping data for table `order_order_status`
--
INSERT INTO `order_order_status` (`id`, `order_status_id`, `order_id`, `created_at`, `updated_at`) VALUES
(1, 2, 1, '2016-10-01 01:57:37', '2016-10-01 01:57:37'),
(2, 2, 2, '2016-10-01 01:57:54', '2016-10-01 01:57:54'),
(3, 2, 3, '2016-10-02 02:12:49', '2016-10-02 02:12:49'),
(4, 6, 3, '2016-10-02 02:14:19', '2016-10-02 02:14:19');
我想選擇是:MAX()+按預期在MySQL不工作
1, 2, 1, '2016-10-01 01:57:37', '2016-10-01 01:57:37'
2, 2, 2, '2016-10-01 01:57:54', '2016-10-01 01:57:54'
4, 6, 3, '2016-10-02 02:14:19', '2016-10-02 02:14:19'
是order_order_status由ORDER_ID
現在的問題分組的最新條目:
運行
select *, max(created_at) from `order_order_status` group by `order_order_status`.`order_id`
返回我:
或PROSA
它返回我不是最新的條目,而是返回一個ORDER_ID 3
MySQL不保證該行將在這種情況下返回。它只是隨機挑選一個。從這個意義上說,它的運作如預期。 [「服務器可以自由選擇每個組中的任何值,因此除非它們相同,否則所選的值是不確定的」](https://dev.mysql.com/doc/refman/5.7/en/group-by -handling.html) – sstan