0

users という名前のデータベーステーブルがあります (25k レコード)

内部には「アバター」という名前のフィールドがあります

このフィールドを更新して、リストから 1 つのランダムな URL を割り当てる必要があります

リストの URL:

http://myserver.com/img/1.png
http://myserver.com/img/2.png
http://myserver.com/img/3.png
http://myserver.com/img/4.png
http://myserver.com/img/5.png
http://myserver.com/img/6.png
http://myserver.com/img/7.png
http://myserver.com/img/8.png
http://myserver.com/img/9.png
http://myserver.com/img/10.png
http://myserver.com/img/11.png
http://myserver.com/img/12.png
http://myserver.com/img/13.png
http://myserver.com/img/14.png
http://myserver.com/img/15.png

私はこのコードを持っています:

UPDATE TABLE SET VALUE=VALUE+ROUND(1+RAND()*4);

このクエリをどのように適応させることができますか?

4

2 に答える 2

1

画像が適切なパターンに従っている場合、@peterm からの回答は適切ですが、リストからランダムな文字列を選択することもできます。

SELECT ELT(ROUND(0.5+RAND()*15), 
 'http://myserver.com/img/1.png',
 'http://myserver.com/img/2.png',
 'http://myserver.com/img/3.png',
 'http://myserver.com/img/4.png',
 'http://myserver.com/img/5.png',
 'http://myserver.com/img/6.png',
 'http://myserver.com/img/7.png',
 'http://myserver.com/img/8.png',
 'http://myserver.com/img/9.png',
 'http://myserver.com/img/10.png',
 'http://myserver.com/img/11.png',
 'http://myserver.com/img/12.png',
 'http://myserver.com/img/13.png',
 'http://myserver.com/img/14.png',
 'http://myserver.com/img/15.png') AS avatar;

http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_eltを参照してください。

ELT()テーブルを更新するには、UPDATE ステートメントで同じ式を使用できます。

UPDATE TABLE SET AVATAR=ELT(...);
于 2013-10-21T18:43:08.080 に答える