• <bdo id='yPUKr'></bdo><ul id='yPUKr'></ul>

  • <small id='yPUKr'></small><noframes id='yPUKr'>

    <i id='yPUKr'><tr id='yPUKr'><dt id='yPUKr'><q id='yPUKr'><span id='yPUKr'><b id='yPUKr'><form id='yPUKr'><ins id='yPUKr'></ins><ul id='yPUKr'></ul><sub id='yPUKr'></sub></form><legend id='yPUKr'></legend><bdo id='yPUKr'><pre id='yPUKr'><center id='yPUKr'></center></pre></bdo></b><th id='yPUKr'></th></span></q></dt></tr></i><div id='yPUKr'><tfoot id='yPUKr'></tfoot><dl id='yPUKr'><fieldset id='yPUKr'></fieldset></dl></div>
      <legend id='yPUKr'><style id='yPUKr'><dir id='yPUKr'><q id='yPUKr'></q></dir></style></legend>
    1. <tfoot id='yPUKr'></tfoot>

        如何在 MySQL 中生成数据?

        How to generate data in MySQL?(如何在 MySQL 中生成数据?)

        <small id='UaHy4'></small><noframes id='UaHy4'>

        <tfoot id='UaHy4'></tfoot>
        • <i id='UaHy4'><tr id='UaHy4'><dt id='UaHy4'><q id='UaHy4'><span id='UaHy4'><b id='UaHy4'><form id='UaHy4'><ins id='UaHy4'></ins><ul id='UaHy4'></ul><sub id='UaHy4'></sub></form><legend id='UaHy4'></legend><bdo id='UaHy4'><pre id='UaHy4'><center id='UaHy4'></center></pre></bdo></b><th id='UaHy4'></th></span></q></dt></tr></i><div id='UaHy4'><tfoot id='UaHy4'></tfoot><dl id='UaHy4'><fieldset id='UaHy4'></fieldset></dl></div>
              <tbody id='UaHy4'></tbody>

              <bdo id='UaHy4'></bdo><ul id='UaHy4'></ul>

                • <legend id='UaHy4'><style id='UaHy4'><dir id='UaHy4'><q id='UaHy4'></q></dir></style></legend>
                  本文介绍了如何在 MySQL 中生成数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  这是我的 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 的日子里也能显示一行.现在,我在没有活动的日子里错过了很多日期.

                  I want a row to show up even for days where there was no ID created. Right now I'm missing a ton of dates for days where there was no activity.

                  关于如何更改此查询以执行此操作有任何想法吗?

                  Any thoughts on how to change this query to do that?

                  推荐答案

                  一个查询就搞定的方法:

                  The way to do it in one query:

                  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"时才需要下一个代码:

                  The next code is only necessary if you want to test and don't have the "my_table" indicated on the question:

                  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');
                  

                  这篇关于如何在 MySQL 中生成数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                      <tbody id='mn7GE'></tbody>

                    <small id='mn7GE'></small><noframes id='mn7GE'>

                        <legend id='mn7GE'><style id='mn7GE'><dir id='mn7GE'><q id='mn7GE'></q></dir></style></legend>

                            <bdo id='mn7GE'></bdo><ul id='mn7GE'></ul>

                          • <tfoot id='mn7GE'></tfoot>
                          • <i id='mn7GE'><tr id='mn7GE'><dt id='mn7GE'><q id='mn7GE'><span id='mn7GE'><b id='mn7GE'><form id='mn7GE'><ins id='mn7GE'></ins><ul id='mn7GE'></ul><sub id='mn7GE'></sub></form><legend id='mn7GE'></legend><bdo id='mn7GE'><pre id='mn7GE'><center id='mn7GE'></center></pre></bdo></b><th id='mn7GE'></th></span></q></dt></tr></i><div id='mn7GE'><tfoot id='mn7GE'></tfoot><dl id='mn7GE'><fieldset id='mn7GE'></fieldset></dl></div>