Jump to content
php.lv forumi

Recommended Posts

Posted

Ir 4 cilveki tabula. Katram vajadzetu pievienot unikaalu skaitli (vietu) no 1 - 4. Un taa lai tas butu random nevis katram cilvekam pec kartas.

Apmeram ta ka te lai vieta butu unikaala un nejausha.

id | vards | vieta

1 andris 3

2 rudis 1

3 maiga 4

4 anda 2

Posted

viens no risinājumiem:

 

mysql_query('UPDATE tabula SET vieta=0');
for($x=1;$x<=4;$x++){
mysql_query(
"UPDATE tabula AS t1 SET 
vieta = $x 
WHERE t1.id IN (
   SELECT id FROM tabula AS t2
   WHERE t2.vieta = 0
   ORDER BY RAND() LIMIT 1
)"
);
}

Posted
viens no risinājumiem:

 

mysql_query('UPDATE tabula SET vieta=0');
for($x=1;$x<=4;$x++){
mysql_query(
"UPDATE tabula AS t1 SET 
vieta = $x 
WHERE t1.id IN (
   SELECT id FROM tabula AS t2
   WHERE t2.vieta = 0
   ORDER BY RAND() LIMIT 1
)"
);
}

 

Kaut kas man tur tomer nedarbojas. Varbut ir vel kaadi citi varianti?

Posted

Ir. Taču tajos citos ir tā pati problēma, ka mēdz "kaut kas" nedarboties.

Pārbaudi, vai vaicājums ir korekts, rakstīju īpaši nepārbaudot...

  • 2 weeks later...
Posted

pamēģini šitā:

 

if(!(mysql_query('UPDATE tabula SET vieta=0'))) die (mysql_error());
for($x=1;$x<=4;$x++){
	if(!(mysql_query(
					"UPDATE tabula AS t1 SET 
					vieta = $x 
					WHERE t1.id IN (
						   SELECT id FROM tabula AS t2
						   WHERE t2.vieta = 0
						   ORDER BY RAND() LIMIT 1)"
	))) die (mysql_error());

}

 

 

Man jau škiet, ka tava tabula nesaucas `tabula`

Posted

Ar RowNum nebūtu vienkāršāk? (nezinu vai pie updeita tas ir pieejams)

 

update tabula set vieta = @rownum
order by rand()

Posted

Paradija shitaadu erroru

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Cik saprotu vainiigs serveris? Varbut ir citi varianti prieksh Client API version 5.0.45

Posted
Cik saprotu vainiigs serveris? Varbut ir citi varianti prieksh Client API version 5.0.45

Client API ir klienta api. Tas nenorāda ne nieka par to, ko uztur vai neuztur serveris. Servera versiju vajag skatītites.

Posted

Beigas uztaisiju shaadi. NEzinu cik tas pareizi bet vismaz darbojas :D

for($x=1;$x<=2;$x++){
	$vieta_query = mysql_query("SELECT id FROM tabula AS t2 WHERE t2.vieta = 0 ORDER BY RAND() LIMIT 1");
	$vieta_row = mysql_fetch_row($vieta_query);
	if(!(mysql_query("UPDATE tabula AS t1 SET vieta = '$x' WHERE t1.id = '$vieta_row[0]'"
	))) die (mysql_error());

×
×
  • Create New...