如何通过时间戳排序从时间戳获取月份和年份的唯一记录

时间:2019-09-17 10:02:42

标签: mysql sql mysql-8.0

Select Date_format(effective_date, "%M %Y") as time 
from articles 
order by effective_date desc limit 10;
+----------------+                                                 
| time           |                                  
+----------------+                                  
| November 2019  |                                  
| November 2019  |                                  
| September 2019 |                                  
| September 2019 |                                  
| August 2019    |                                  
| July 2019      |                                  
| June 2019      |                                  
| May 2019       |                                  
| April 2019     |                                  
| March 2019     |                                  
+----------------+  

表结构

CREATE TABLE `articles` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `author_id` bigint(20) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `article_page_details_count` int(11) DEFAULT NULL,
  `effective_date` datetime DEFAULT NULL,
  `position` int(11) DEFAULT NULL,
  `hits` int(11) DEFAULT NULL,
  `status` int(11) DEFAULT '0',
  `deleted_at` datetime DEFAULT NULL,
  `created_by_admin_user_id` bigint(20) DEFAULT NULL,
  `updated_by_admin_user_id` bigint(20) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `index_articles_on_author_id` (`author_id`),
  KEY `index_articles_on_created_by_admin_user_id` (`created_by_admin_user_id`),
  KEY `index_articles_on_updated_by_admin_user_id` (`updated_by_admin_user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=113 DEFAULT CHARSET=utf8

我只想获取唯一记录。 与众不同不起作用,如果我使用分组依据,则订单无效。

mysql版本是8.0

不同一起出现错误。

Select distinct(Date_format(effective_date, "%M %Y")) as time 
from articles 
order by effective_date desc limit 10;
  

错误3065(HY000):ORDER BY子句的表达式#1不在SELECT中   列表,引用不在其中的列“ articles.effective_date”   SELECT列表;这与DISTINCT不兼容

4 个答案:

答案 0 :(得分:0)

以下是使用GROUP BY的解决方案:

select date_format(effective_date, '%Y %M') new_effective_date
from articles
group by year(effective_date), month(effective_date),  date_format(effective_date, '%Y %M')
order by year(effective_date) desc, month(effective_date) desc
limit 10

Demo on DB Fiddle

with articles as (
      select '2018-01-01' effective_date
      union all select '2018-01-01' 
      union all select '2018-02-01' 
      union all select '2018-03-01' 
 )
select date_format(effective_date, '%Y %M') new_effective_date
from articles
group by year(effective_date), month(effective_date), date_format(effective_date, '%Y %M')
order by year(effective_date) desc, month(effective_date) desc
limit 10;

| new_effective_date |
| ------------------ |
| 2018 March         |
| 2018 February      |
| 2018 January       |

答案 1 :(得分:0)

在将DISTINCT子句与ORDER BY一起使用时,您需要确保在ORDER BY子句中也选择用于SELECT的字段。

现在,您正在将日期时间值(Y-m-d h:i:s)格式转换为(M Y),并使用它进行排序。但这不会给您正确的结果,因为2019-01-01 < 2019-04-01,但是当转换为自定义字符串格式时,它将得到January 2019 < April 2019,因为A的字符数小于J比较。

因此,一种令人费解的方法可能是使用Str_To_Date()函数将自定义的“ Month Year”字符串转换回日期格式(Y-m-d)以进行正确的排序:

模式(MySQL v8.0)

create table articles (`effective_date` datetime DEFAULT NULL);

insert into articles values ('2019-01-05');
insert into articles values ('2019-01-06');
insert into articles values ('2019-02-05');

查询#1

SELECT DISTINCT Date_format(effective_date, "%M %Y") as time_MY 
FROM articles 
ORDER BY STR_TO_DATE(CONCAT(time_MY, ' 1'), '%M %Y %d') desc 
limit 10;

结果

| time_MY       |
| ------------- |
| February 2019 |
| January 2019  |

View on DB Fiddle

答案 2 :(得分:0)

您可能会对此获得帮助

 Select distinct Date_format(effective_date, "%M %Y") as time 
    from articles 
    order by effective_date desc limit 10;

答案 3 :(得分:0)

我建议使用这样的group byorder by

select Date_format(effective_date, '%M %Y') as time 
from articles 
group by time
order by min(effective_date) desc
limit 10;

这使您可以:

  • 仅选择一列
  • 具有唯一的值
  • 按实际日期排序,而不是字符串形式