gpt4 book ai didi

mysql - 统计mySQL中某月内的数据

转载 作者:行者123 更新时间:2023-11-29 07:21:38 24 4
gpt4 key购买 nike

您好,我在按月对图像数据进行排序时遇到问题,我希望能够通过按月获取计数和分组来了解在特定月份内创建、更新和删除了多少图像。我有两个包含图像数据的表。两个表都有“createdAt”、“updatedAt”和“deletedAt”列,所以我所做的是对两个表的列进行日期格式化并将它们联合起来以将所有数据放在一起,但我是无法获取数据计数。这是我目前所拥有的:

SELECT DATE_FORMAT(`createdAt`, "%M %Y"), DATE_FORMAT(`updatedAt`, "%M %Y"), DATE_FORMAT(`deletedAt`, "%M %Y") FROM harvest_sample_images UNION SELECT DATE_FORMAT(`createdAt`, "%M %Y"), DATE_FORMAT(`updatedAt`, "%M %Y"), DATE_FORMAT(`deletedAt`, "%M %Y") FROM plant_sample_images

如有任何帮助,我们将不胜感激。

SELECT DATE_FORMAT(`createdAt`, "%M %Y") DAT, count(*) AS `created`,
(SELECT DATE_FORMAT(`updatedAt`, "%M %Y") DAT, COUNT(*)) AS `updated`,
(SELECT DATE_FORMAT(`deletedAt`, "%M %Y") DAT COUNT(*)) AS `deleted`
FROM (SELECT `createdAt`, `updatedAt`, `deletedAt` FROM harvest_sample_images UNION SELECT `createdAt`, `updatedAt`, `deletedAt` FROM plant_sample_images)
GROUP BY DATE_FORMAT(`created`, "M% %Y")

我已经试过了。


所以我可能找到了另一种方法来将图像信息分类到每个相应的月份以及计数以及显示它所属的月份。

SELECT DATE_FORMAT(`timestamp_date`, "%M %Y") AS `Month`,
(SELECT COUNT(*) FROM (SELECT `createdAt` FROM harvest_sample_images UNION ALL SELECT `createdAt` FROM plant_sample_images) AS `images_created` WHERE DATE_FORMAT(images_created.`createdAt`, "%m %y") = DATE_FORMAT(`timestamp_date`, "%m %y")) AS `Created`,
(SELECT COUNT(*) FROM (SELECT `updatedAt` FROM harvest_sample_images UNION ALL SELECT `updatedAt` FROM plant_sample_images) AS `images_updated` WHERE DATE_FORMAT(images_updated.`updatedAt`, "%m %y") = DATE_FORMAT(`timestamp_date`, "%m %y")) AS `Updated`,
(SELECT COUNT(*) FROM (SELECT `deletedAt` FROM harvest_sample_images UNION ALL SELECT `deletedAt` FROM plant_sample_images) As `images_deleted` WHERE DATE_FORMAT(images_deleted.`deletedAt`, "%m %y") = DATE_FORMAT(`timestamp_date`, "%m %y")) AS `Deleted`
FROM table_dates

所以我现在正在做的是,我创建了另一个表,其中包含从数据开始到现在的日期列表。然后我循环遍历数据并检查 table_dates 的格式化日期是否等于两个图像表日期并集的格式化日期。我得到计数​​并将其添加为新表中的一列。思想受到赞赏

最佳答案

你需要计数和分组

select  year(createdAt) year , month(createdAt) month, count(*)
from my_table
group by year(createdAt), month(createdAt)

SELECT DATE_FORMAT(`createdAt`, "%M %Y")  DAT, count(*)
from my_table
group by DATE_FORMAT(`createdAt`, "%M %Y")

对于 3 个计数,您可以使用 join

select t1.year_month, t1.count_create, t2.count_update, t3count_delete
from (
SELECT DATE_FORMAT(`createdAt`, "%M %Y") year_month , count(*) count_create
from my_table
group by DATE_FORMAT(`createdAt`, "%M %Y")
) t1
left join (
SELECT DATE_FORMAT(`updatedAt`, "%M %Y") year_month , count(*) count_update
from my_table
group by DATE_FORMAT(`updatedAt`, "%M %Y")

) t2 ON t1.year_month = t2.year_month
left join (
SELECT DATE_FORMAT(`deletedAt`, "%M %Y") year_month , count(*) count_delete
from my_table
group by DATE_FORMAT(`deletedAt`, "%M %Y")

) t3 ON t1.year_month = t3.year_month

关于mysql - 统计mySQL中某月内的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56019539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com