Jump to content
php.lv forumi

mysql update


sidrs

Recommended Posts

hmmm kāpēc ciklu?

Man būs 4 useri ar idiem 1, 2, 3, 4 vienā piegājienā laboju 2 un 4.

 

mysql> create table users (id integer, rnd integer);
Query OK, 0 rows affected (0.16 sec)

mysql> insert into users values (1, null);
Query OK, 1 row affected (0.03 sec)

mysql> insert into users values (2, null);
Query OK, 1 row affected (0.03 sec)

mysql> insert into users values (3, null);
Query OK, 1 row affected (0.05 sec)

mysql> insert into users values (4, null);
Query OK, 1 row affected (0.03 sec)

mysql> update users set rnd = rand()*10000
   -> where id in (2, 4);
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from users;
+------+------+
| id   | rnd  |
+------+------+
|    1 | NULL |
|    2 |  841 |
|    3 | NULL |
|    4 | 7387 |
+------+------+
4 rows in set (0.00 sec)

 

Gints Plivna

http://datubazes.wordpress.com/

Link to comment
Share on other sites

$rand1 = rand(1,5);
$rand2 = rand(1,5);
$rand3 = rand(1,5);
$rand4 = rand(1,5);
$rand5 = rand(1,5);
mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand1)."' WHERE id = '".mysql_real_escape_string($rand1)."'") or die(mysql_error());
mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand2)."' WHERE id = '".mysql_real_escape_string($rand2)."'") or die(mysql_error());
mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand3)."' WHERE id = '".mysql_real_escape_string($rand3)."'") or die(mysql_error());
mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand4)."' WHERE id = '".mysql_real_escape_string($rand4)."'") or die(mysql_error());
mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand5)."' WHERE id = '".mysql_real_escape_string($rand5)."'") or die(mysql_error());

Link to comment
Share on other sites

njaa chizij, no kura laika skaitļus liek pēdiņās un vēl eskeipo ar mysql_real_escape_string()??? :D:D:D

 

 

Gints Plivna +1

taču ja pieciem useriem vajag updeitot 5 nelielus random skaitļus, teiksim 1..5, tad jau gan nāksies rakstīt ar php 5 kverijus turklāt ņemt secīgus skaitļus ārā no masīva ar array_rand(), jo pie tik maza random skaitļu intervāla ir liela varbūtītba, ka tie atkārtosies

 

piemēram, pēc chizija (latviski laikam būtu "sieriņš", ja tas ir latviskots angļu cheese?) varianta man nupat sanāca 2, 4, 4, 4, 4 (labi, ka neatkārtojas :D:D:D)

Edited by 2easy
Link to comment
Share on other sites

taču ja pieciem useriem vajag updeitot 5 nelielus random skaitļus, teiksim 1..5, tad jau gan nāksies rakstīt ar php 5 kverijus turklāt ņemt secīgus skaitļus ārā no masīva ar array_rand(), jo pie tik maza random skaitļu intervāla ir liela varbūtītba, ka tie atkārtosies

 

Think out of the box (php) :P

 

Lietojam to pašu tabulu users.

Taisu updeitu 2 reizes arī atlasot datus, lai pierādītu, ka ir random (cik nu labs ir random ģenerētājs mysqlā iebūvēts) un rezultāti nesakrīt.

mysql> set @i = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> update users set rnd = (@i := @i + 1)
   -> order by rand();
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from users;
+------+------+
| id   | rnd  |
+------+------+
|    1 |    3 |
|    2 |    1 |
|    3 |    2 |
|    4 |    4 |
+------+------+
4 rows in set (0.00 sec)

mysql> set @i = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> update users set rnd = (@i := @i + 1)
   -> order by rand();
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from users;
+------+------+
| id   | rnd  |
+------+------+
|    1 |    1 |
|    2 |    4 |
|    3 |    3 |
|    4 |    2 |
+------+------+
4 rows in set (0.00 sec)

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

Gints Plivna +1000 :))

oo šis ir smuks mysql variabļu pielietojums!!! turklāt vēl tādā kombinācijā ar ORDER BY RAND()

vnk woooow

 

good job! ;)

 

P.S.

protams, ir smuki, ka šāds daudzu rindu update ir noslēpts vienā kverijā, taču tehniski no performances viedokļa tas varētu būt tikai nedaudz ātrāk kā pēc indeksēta id apdeitot katru useri atsevišķi. bet risinājums anyway ir loti elegants!

Link to comment
Share on other sites

Esu iesacējs un daudzkas ir tumša bilde :)

bet nu kautko sagrābstot piespiedu updeitot katram random ciparu,tik nezinu cik tas ir pareizi :)

 

	
$result = mysql_query("SELECT id FROM users where atlase='1'");
   while ($row = mysql_fetch_row($result))
   {
$user1=$row[0];
$user2=$row[1];
$user3=$row[2];
$user4=$row[3];
$user5=$row[4];
$user6=$row[5];

 $rand1=rand(1,100);
 $rand2=rand(1,100);
 $rand3=rand(1,100);
 $rand4=rand(1,100);
 $rand5=rand(1,100);
 $rand6=rand(1,100);

        mysql_query("UPDATE users SET aile=$rand1 WHERE id='$user1'");
 mysql_query("UPDATE users SET aile=$rand2 WHERE id='$user2'");
 mysql_query("UPDATE users SET aile=$rand3 WHERE id='$user3'");
 mysql_query("UPDATE users SET aile=$rand4 WHERE id='$user4'");
 mysql_query("UPDATE users SET aile=$rand5 WHERE id='$user5'");
 mysql_query("UPDATE users SET aile=$rand6 WHERE id='$user6'");
}

 

kritiku studijā :D

Link to comment
Share on other sites

cik sapratu tad šitie nosaka kads userim id

 

        $user1=$row[0];
       $user2=$row[1];
       $user3=$row[2];
       $user4=$row[3];
       $user5=$row[4];
       $user6=$row[5];

 

un tad pec id liek random skaitļus iekša..

 

id kolona ir viena un tur nekas nav mainijies un skaitli aile ielikas katram cits..

Link to comment
Share on other sites

cik sapratu tad šitie nosaka kads userim id

 

        $user1=$row[0];
       $user2=$row[1];
       $user3=$row[2];
       $user4=$row[3];
       $user5=$row[4];
       $user6=$row[5];

 

un tad pec id liek random skaitļus iekša..

 

id kolona ir viena un tur nekas nav mainijies un skaitli aile ielikas katram cits..

 

Bet ar mysql_fetch_row tu tak nolasi VIENU ierakstu, nevis veselus 6...

 

Varbūt labāk izmanot mysql_fetch_assoc, būs skaidrāka padarīšana

 

while($row = mysql_fetch_assoc($data){
$user_id  = $row['id'];
//...
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...