白嫖大家的代码, Mysql 如何按月份调用,或是如何设计数据库...

2022-03-04 20:14:04 +08:00
 enzo1205

最终的效果是要这样,以此类推

. . 2022 1 月 2 月 3 月 4 月 5 月 6 月 7 月 8 月 9 月 10 月 11 月 12 月 . . .

博客只有一个时间字段..我现在可以调出来,系统里面有的文章.. 比如 2022 1 月 2 月 3 月... 但是我想弄完整的月份,有文章话,该月份可以点击,没有文章的月份不能点击... 麻烦大家给个思路.... 谢谢

1050 次点击
所在节点    MySQL
3 条回复
mineralsalt
2022-03-04 20:32:33 +08:00
按月统计记录数量

SELECT
from_unixtime( create_time, "%Y-%m" ) AS months,
count( id ) AS counts
FROM
`table`
GROUP BY
months;
actar
2022-03-04 20:54:08 +08:00
select concat('2022', '-', months.month) as time, count(articles.id) > 0 as exist
from (
select '01' as month
union
select '02' as month
union
select '03' as month
union
select '04' as month
union
select '04' as month
union
select '06' as month
union
select '07' as month
union
select '08' as month
union
select '09' as month
union
select '10' as month
union
select '11' as month
union
select '12' as month
) as months
left join articles on month(created_at) = months.month and year(articles.created_at) = '2022'
group by time;


随便写的,不保证任何效率
rabbbit
2022-03-04 21:42:07 +08:00
CREATE TABLE IF NOT EXISTS `blog` (
`id` int NOT NULL DEFAULT '0',
`title` varchar(100) NOT NULL DEFAULT '',
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`content` mediumblob
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

SELECT from_unixtime(UNIX_TIMESTAMP(createTime), '%Y 年%m 月') TIME FROM blog GROUP BY time

SELECT
*,
from_unixtime(UNIX_TIMESTAMP(createTime), '%Y 年%m 月') TIME,
ROW_NUMBER() over (partition by from_unixtime(UNIX_TIMESTAMP(createTime), '%Y-%m') ORDER BY createTime) num
FROM blog

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/838052

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX