2017-09-07 5 views
0

私はビルドしたばかりのショーケースデモプロジェクト用にこのダミーテーブルを手に入れました。したがって、順番に投稿をアップロードし始めました。ランダムな日付に変更しますか?既存のすべてのエントリのdatetime列の値をランダム化

title | content | date 
title1 | xxxxxxxxx | 2017-09-07 16:49:57 
title2 | xxxxxxxxx | 2017-09-07 16:49:57 

そう...私は何をしたいのですが、日付カラムはあなたがPRIMARY KEY(すなわちid)を持っている必要があり

title | content | date 
title1 | xxxxxxxxx | 2017-06-06 12:13:01 <- random generated date 
title2 | xxxxxxxxx | 2017-19-07 21:37:57 <- random generated date 
+0

https://mockaroo.com/に使える。 – jarlh

答えて

1

のようなものをとどまるように、単純にクエリを実行することですへ自己結合更新クエリでこれを行います。

ない場合は、行を一致させる代わりにtitleを使用することができますが、それは同じランダム日付で終わる同じタイトルを持つすべての記事につながる:

/* convert date range to seconds to get an INT to express randomization range */ 
SET @min := UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2009 12:00AM', '%M %d %Y %h:%i%p')); 
/* subtract the max ID from the range so we can add the ID later to the range 
* without getting out of range values */ 
SET @max := UNIX_TIMESTAMP(STR_TO_DATE('Sep 08 2017 01:00AM', '%M %d %Y %h:%i%p')) - (SELECT MAX(id) FROM table); 

/* join a second reference of the table as a source for the update */ 
UPDATE table AS target JOIN table AS source ON source.id = target.id 
/* and add the source ID to the range to force the optimizer to calculate 
* a random number for each row independently */ 
SET target.date = FROM_UNIXTIME(ROUND((RAND()*(@max - @min + source.id))[email protected])) 
/* dont forget to link source and destination with the primary key! */ 
WHERE source.id = target.id 
関連する問題