1. kmotoki

    Posted

    kmotoki
Changes in title
+MySQLで 2017/02/01 <= x < 2017/02/08 のランダムな日時を取得するクエリ
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,38 @@
+```sql
+SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01')));
+```
+実行結果
+
+```sql
+mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01')));
++----------------------------------------------------------------------------------------------------------------------+
+| FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01'))) |
++----------------------------------------------------------------------------------------------------------------------+
+| 2017-02-05 08:20:52.189146 |
++----------------------------------------------------------------------------------------------------------------------+
+1 row in set (0.00 sec)
+
+mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01')));
++----------------------------------------------------------------------------------------------------------------------+
+| FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01'))) |
++----------------------------------------------------------------------------------------------------------------------+
+| 2017-02-02 23:37:49.727271 |
++----------------------------------------------------------------------------------------------------------------------+
+1 row in set (0.00 sec)
+
+mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01')));
++----------------------------------------------------------------------------------------------------------------------+
+| FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01'))) |
++----------------------------------------------------------------------------------------------------------------------+
+| 2017-02-04 21:06:32.087503 |
++----------------------------------------------------------------------------------------------------------------------+
+1 row in set (0.00 sec)
+
+mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01')));
++----------------------------------------------------------------------------------------------------------------------+
+| FROM_UNIXTIME(UNIX_TIMESTAMP('2017-02-01') + RAND() * (UNIX_TIMESTAMP('2017-02-08') - UNIX_TIMESTAMP('2017-02-01'))) |
++----------------------------------------------------------------------------------------------------------------------+
+| 2017-02-07 10:39:11.274294 |
++----------------------------------------------------------------------------------------------------------------------+
+1 row in set (0.00 sec)
+```