统计mySQL中某月的数据
Counting data falling within certain month in mySQL
您好,我在按月对图像数据进行排序时遇到问题,我希望能够通过按月获取计数和分组来了解在特定月份内创建、更新和删除了多少图像。我有两个带有图像数据的 table。两个 table 都有 'createdAt'、'updatedAt' 和 'deletedAt' 列,所以我所做的是日期格式的列tables 并将它们结合在一起以将所有数据放在一起,但我在获取数据计数时遇到了问题。这是我目前所拥有的:
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,其中包含从数据开始到现在的日期列表。然后我遍历数据并检查 table_dates 的格式化日期是否等于两个图像 tables 日期并集的格式化日期。我获取计数并将其添加为新 table 中的一列。
感谢您的想法
您需要根据
进行计数和分组
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
您好,我在按月对图像数据进行排序时遇到问题,我希望能够通过按月获取计数和分组来了解在特定月份内创建、更新和删除了多少图像。我有两个带有图像数据的 table。两个 table 都有 'createdAt'、'updatedAt' 和 'deletedAt' 列,所以我所做的是日期格式的列tables 并将它们结合在一起以将所有数据放在一起,但我在获取数据计数时遇到了问题。这是我目前所拥有的:
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,其中包含从数据开始到现在的日期列表。然后我遍历数据并检查 table_dates 的格式化日期是否等于两个图像 tables 日期并集的格式化日期。我获取计数并将其添加为新 table 中的一列。 感谢您的想法
您需要根据
进行计数和分组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