LoginSignup
0
0

More than 3 years have passed since last update.

MySQLでランダムデータを作成する

Posted at

日付

select date(now() - interval 1 day) + interval floor(rand() * 86400) second;

実行時刻の1日前の00:00:00から、その日内の時刻でランダムな日時を決める。

mysql> select date(now() - interval 1 day) + interval floor(rand() * 86400) second;
+----------------------------------------------------------------------+
| date(now() - interval 1 day) + interval floor(rand() * 86400) second |
+----------------------------------------------------------------------+
| 2021-04-17 01:32:41                                                  |
+----------------------------------------------------------------------+
1 row in set (0.01 sec)

文字列

substring(md5(rand()), 1, 19)

ランダムで生成したmd5の文字列から、所定の長さを使う。

mysql> select substring(md5(rand()), 1, 19);
+-------------------------------+
| substring(md5(rand()), 1, 19) |
+-------------------------------+
| 08384853ae61dc665e5           |
+-------------------------------+
1 row in set (0.00 sec)

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0