简体   繁体   English

Max()+组无法按预期在mysql中工作

[英]Max() + group by not working as expected in mysql

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');

What i want to select is: 我要选择的是:

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'

that is the newest entry of order_order_status grouped by order_id 这是由order_id分组的order_order_status的最新条目

now the problem: 现在的问题:

running 赛跑

select *, max(created_at) from `order_order_status` group by `order_order_status`.`order_id`

returns me: 返回我:

在此处输入图片说明

or in prosa 或在prosa

it returns me NOT the newest entry, instead it returns the older one for order_id 3 它返回的不是我的最新条目,而是返回较旧的order_id 3条目

MySQL is working exactly as expected. MySQL 完全按预期工作。 The problem is your expectations. 问题是您的期望。

select * with a group by doesn't make sense. group by select *没有任何意义。 You want to get the maximum, do something like this: 您想要获得最大的最大值,请执行以下操作:

select oos.*
from order_order_status
where oos.created_at = (select max(oos2.created_at)
                        from order_order_status oos2
                        where oos2.order_id = oos.order_id
                       );

Aggregation ( group by ) produces one row per group. 聚合( group bygroup by产生一行。 An aggregation function such as max() gets the maximum value of a column -- nothing more. 诸如max()类的聚合函数获取列的最大值-仅此而已。 It just operates on a column. 它仅在列上运行。

When you use select * , you have a bunch of columns that are not in the group by and not the arguments to aggregation columns. 使用select * ,会有一堆不在group by的列,也不是聚合列的参数。 MySQL allows this syntax (unfortunately -- few other databases do). MySQL允许使用这种语法(不幸的是,其他数据库很少这样做)。 The values for the unaggregated columns are arbitrary values from indeterminate rows in the group. 未聚合列的值是组中不确定行中的任意值。

通过order_id desc使用order可以解决您的问题

select *, max(created_at) from `order_order_status` group by `order_order_status`.`order_id` order by `order_order_status`.`order_id` desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM