mysql - 如何使用时间戳排序从时间戳中获取带有月份和年份的唯一记录

标签 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

我只想获取唯一记录。 distinct 不起作用,如果我使用 group by 则 order 不起作用。

mysql版本为8.0

使用 Distinct 我遇到了错误。

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

ERROR 3065 (HY000): Expression #1 of ORDER BY clause is not in SELECT list, references column 'articles.effective_date' which is not in SELECT list; this is incompatible with DISTINCT

最佳答案

这是一个使用 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       |

关于mysql - 如何使用时间戳排序从时间戳中获取带有月份和年份的唯一记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57971908/

相关文章:

c# - 在 Fluent NHibernate 中创建数据库期间列名发生变化

mysql - 从另一个表中选择具有备用有序字段的行

PHP 和 Javascript

MySQL 8.0 lower_case_table_names 1

php - XAMPP 本地主机与 mysql 的问题

老版本的mysql字符集编码

MySQL 表数据目录大于从 information_schema.tables 计算的大小

php/mysql 标签查询模式

mysql - 想要将十六进制转换为二进制,给出的答案超过 64 位

MySQL JSON_SET() 处理空数组