开发者社区> 问答> 正文

如何在MySQL中生成数据??mysql

这是我的SQL:

SELECT COUNT(id), CONCAT(YEAR(created_at), '-', MONTH(created_at), '-', DAY(created_at)) FROM my_table GROUP BY YEAR(created_at), MONTH(created_at), DAY(created_at) 我希望即使没有创建ID的日子也能显示行。现在,在没有活动的日子里,我错过了很多约会。

关于如何更改此查询来做到这一点的任何想法?

展开
收起
保持可爱mmm 2020-05-17 18:11:33 436 0
1 条回答
写回答
取消 提交回答
  • 在一个查询中执行此操作的方法:

    SELECT COUNT(my_table.id) AS total, CONCAT(YEAR(dates.ddate), '-', MONTH(dates.ddate), '-', DAY(dates.ddate)) FROM ( -- Creates "on the fly" 65536 days beginning from 2000-01-01 (179 years) SELECT DATE_ADD("2000-01-01", INTERVAL (b1.b + b2.b + b3.b + b4.b + b5.b + b6.b + b7.b + b8.b + b9.b + b10.b + b11.b + b12.b + b13.b + b14.b + b15.b + b16.b) DAY) AS ddate FROM (SELECT 0 AS b UNION SELECT 1) b1, (SELECT 0 AS b UNION SELECT 2) b2, (SELECT 0 AS b UNION SELECT 4) b3, (SELECT 0 AS b UNION SELECT 8) b4, (SELECT 0 AS b UNION SELECT 16) b5, (SELECT 0 AS b UNION SELECT 32) b6, (SELECT 0 AS b UNION SELECT 64) b7, (SELECT 0 AS b UNION SELECT 128) b8, (SELECT 0 AS b UNION SELECT 256) b9, (SELECT 0 AS b UNION SELECT 512) b10, (SELECT 0 AS b UNION SELECT 1024) b11, (SELECT 0 AS b UNION SELECT 2048) b12, (SELECT 0 AS b UNION SELECT 4096) b13, (SELECT 0 AS b UNION SELECT 8192) b14, (SELECT 0 AS b UNION SELECT 16384) b15, (SELECT 0 AS b UNION SELECT 32768) b16 ) dates LEFT JOIN my_table ON dates.ddate = my_table.created_at GROUP BY dates.ddate ORDER BY dates.ddate 仅当您要测试并且没有在问题上指示“ my_table”时,才需要下一个代码:

    create table my_table ( id int (11), created_at date ); insert into my_table (id, created_at) values('1','2000-01-01'); insert into my_table (id, created_at) values('2','2000-01-01'); insert into my_table (id, created_at) values('3','2000-01-01'); insert into my_table (id, created_at) values('4','2001-01-01'); insert into my_table (id, created_at) values('5','2100-06-06');来源:stack overflow

    2020-05-17 18:11:54
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像