Jump to content
php.lv forumi

no 1 līdz 30


nascar

Recommended Posts

Es nebūtu tik pārliecināts, ka tiek izmantota datubāze.

Lai arī man nav līdz galam skaidra visa ideja.

Pāris jautājumus laikam prasās:

a) tā izloze ir vienreizēja tiem 30 dalībniekiem un faktiski pēc turnīra, vai nu kas tur notiks, tos kārtas numurus nevajag nekur?

b) vai to visu darīs vairākās "pakās" pa 30 uzreiz vienlaikus?

Bet tā jau mierīgi var uzģenerēt masīvu un pēc kārtas piešķirt jau no tā, katram kurš piesakās uzreiz.

Tā vai tad nav ērtāk, glabājas failā jau saģenerēti 30 rindiņās randomā skaitļi un katru nākamo dalībnieku vienkārši pieraksta klāt.

Ar datubāzi to var tieši tā pat, ja izmanto.

Palaiž turnīru, saģenerējas tie 30 skaitļi randomā, piesakās dalībnieki, kuriem katram pēc kārtas dod vienu skaitli un mauc tik vaļā:)

Edited by mounkuls
Link to comment
Share on other sites

Nesaku ka tā obligāti jādara, bet tā kā man patīk SQL, tad :)

Ja izmanto MySQL, tad tīri mysqlisks risinājums:

1. Izveidojam tabulu

2. Aizpildam 30 rindiņas ar jaunu id, bez nekā cita.

 

mysql> create table u (id int not null auto_increment primary key,
   ->   name varchar(10), surname varchar(10));
Query OK, 0 rows affected (0.08 sec)

mysql> insert into u (name) select null from information_schema.global_variables

   -> limit 30;
Query OK, 30 rows affected (0.03 sec)
Records: 30  Duplicates: 0  Warnings: 0

mysql> select * from u;
+----+------+---------+
| id | name | surname |
+----+------+---------+
|  1 | NULL | NULL    |
|  2 | NULL | NULL    |
|  3 | NULL | NULL    |
|  4 | NULL | NULL    |
|  5 | NULL | NULL    |
|  6 | NULL | NULL    |
|  7 | NULL | NULL    |
|  8 | NULL | NULL    |
|  9 | NULL | NULL    |
| 10 | NULL | NULL    |
| 11 | NULL | NULL    |
| 12 | NULL | NULL    |
| 13 | NULL | NULL    |
| 14 | NULL | NULL    |
| 15 | NULL | NULL    |
| 16 | NULL | NULL    |
| 17 | NULL | NULL    |
| 18 | NULL | NULL    |
| 19 | NULL | NULL    |
| 20 | NULL | NULL    |
| 21 | NULL | NULL    |
| 22 | NULL | NULL    |
| 23 | NULL | NULL    |
| 24 | NULL | NULL    |
| 25 | NULL | NULL    |
| 26 | NULL | NULL    |
| 27 | NULL | NULL    |
| 28 | NULL | NULL    |
| 29 | NULL | NULL    |
| 30 | NULL | NULL    |
+----+------+---------+
30 rows in set (0.00 sec)

 

 

3. Kad vajag pieliekt jaunu personu ņemam random 1 rindu, kura vēl ir tukša un iemetam tur personu.

 

mysql> update u set name = 'a', surname = 'a'
   -> where name is null
   -> order by rand() limit 1;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update u set name = 'b', surname = 'b'
   -> where name is null
   -> order by rand() limit 1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update u set name = 'c', surname = 'c'
   -> where name is null
   -> order by rand() limit 1;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from u;
+----+------+---------+
| id | name | surname |
+----+------+---------+
|  1 | NULL | NULL    |
|  2 | NULL | NULL    |
|  3 | NULL | NULL    |
|  4 | NULL | NULL    |
|  5 | NULL | NULL    |
|  6 | NULL | NULL    |
|  7 | NULL | NULL    |
|  8 | NULL | NULL    |
|  9 | NULL | NULL    |
| 10 | a    | a       |
| 11 | NULL | NULL    |
| 12 | NULL | NULL    |
| 13 | NULL | NULL    |
| 14 | b    | b       |
| 15 | NULL | NULL    |
| 16 | NULL | NULL    |
| 17 | c    | c       |
| 18 | NULL | NULL    |
| 19 | NULL | NULL    |
| 20 | NULL | NULL    |
| 21 | NULL | NULL    |
| 22 | NULL | NULL    |
| 23 | NULL | NULL    |
| 24 | NULL | NULL    |
| 25 | NULL | NULL    |
| 26 | NULL | NULL    |
| 27 | NULL | NULL    |
| 28 | NULL | NULL    |
| 29 | NULL | NULL    |
| 30 | NULL | NULL    |
+----+------+---------+
30 rows in set (0.00 sec)

 

4. Kad viss ir aizpildīts, tad neviena rinda vairs nemainīsies (pievēršama uzmanību, ka pirms tam bija changed un matched 1, tagad 0):

mysql> update u set name = 'a', surname = 'a';
Query OK, 29 rows affected (0.03 sec)
Rows matched: 30  Changed: 29  Warnings: 0

mysql> update u set name = 'c', surname = 'c'
   -> where name is null
   -> order by rand() limit 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

 

Gints Plivna

http://datubazes.wordpress.com

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...