純粋なSQLでこれを実行できるアプローチがありますが、制限があります。
最初に、行として数列1、2、3 ... nを設定する必要があります(それをselect row from rows
返すと仮定します)。
次に、これに参加したままにして、最小から最大までの日数に基づいて日付に変換できます。
select @min_join_on := (select min(join_on) from user);
select @no_rows := (select datediff(max(join_on), @min_join_on) from user)+1;
必要な行数が表示され、これを使用して次のことができます。
select adddate(@min_join_on, interval row day) from rows where row <= @no_rows;
必要な日付のシーケンスが返されます。その後、左結合を実行してユーザーテーブルに戻すことができます。
サブクエリを使用すると、変数の使用を回避できます。読みやすくするために分解しました。
ここで問題となるのは、テーブルの行数がrows
@no_rowsよりも大きくなければならないことです。10,000行の場合、最大27年の日付範囲で作業でき、100,000行の場合、最大273年の日付範囲で作業できます(これは非常に気分が悪いですが、ストアドプロシージャを使用したくない場合は、恐れ入ります。見た目も違和感もあります)。
したがって、このような固定の日付範囲で作業できる場合は、テーブルを次のようなクエリに置き換えることもできます。
SELECT @row := @row + 1 as row FROM (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4, (SELECT @row:=0) r
これにより、1から10,000までの10,000行が生成され、ひどく非効率になることはありません。
したがって、最終的には1つのクエリで実行できます。
create table user(id INT NOT NULL AUTO_INCREMENT, name varchar(100), join_on date, PRIMARY KEY(id));
mysql> select * from user;
+----+-------+------------+
| id | name | join_on |
+----+-------+------------+
| 1 | user1 | 2010-04-02 |
| 2 | user2 | 2010-04-04 |
| 3 | user3 | 2010-04-08 |
| 4 | user4 | 2010-04-08 |
+----+-------+------------+
4 rows in set (0.00 sec)
insert into user values (null, 'user1', '2010-04-02'), (null, 'user2', '2010-04-04'), (null, 'user3', '2010-04-08'), (null, 'user4', '2010-04-08')
SELECT date, count(id)
FROM (
SELECT adddate((select min(join_on) from user), row-1) as date
FROM (
SELECT @row := @row + 1 as row FROM (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, (SELECT @row:=0) r ) n
WHERE n.row <= ( select datediff(max(join_on), min(join_on)) from user) + 1
) dr LEFT JOIN user u ON dr.date = u.join_on
GROUP BY dr.date
+------------+-----------+
| date | count(id) |
+------------+-----------+
| 2010-04-02 | 1 |
| 2010-04-03 | 0 |
| 2010-04-04 | 1 |
| 2010-04-05 | 0 |
| 2010-04-06 | 0 |
| 2010-04-07 | 0 |
| 2010-04-08 | 2 |
+------------+-----------+
7 rows in set (0.00 sec)