shurix Posted June 6, 2008 Report Share Posted June 6, 2008 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 Link to comment Share on other sites More sharing options...
Aleksejs Posted June 6, 2008 Report Share Posted June 6, 2008 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 )" ); } Link to comment Share on other sites More sharing options...
andrisp Posted June 6, 2008 Report Share Posted June 6, 2008 (edited) http://dev.mysql.com/doc/refman/5.0/en/mat...l#function_rand Ah viņam savādāk vajadzēja. Edited June 6, 2008 by andrisp Link to comment Share on other sites More sharing options...
shurix Posted June 6, 2008 Author Report Share Posted June 6, 2008 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? Link to comment Share on other sites More sharing options...
Aleksejs Posted June 6, 2008 Report Share Posted June 6, 2008 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... Link to comment Share on other sites More sharing options...
shurix Posted June 19, 2008 Author Report Share Posted June 19, 2008 Jajebuu nevaru izdomaat kapec neiet. Link to comment Share on other sites More sharing options...
Alnis Posted June 19, 2008 Report Share Posted June 19, 2008 Kādu kļūdu izmet? Link to comment Share on other sites More sharing options...
shurix Posted June 20, 2008 Author Report Share Posted June 20, 2008 Nekaadu vienkarshi neupdato vietu. Link to comment Share on other sites More sharing options...
Alnis Posted June 20, 2008 Report Share Posted June 20, 2008 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` Link to comment Share on other sites More sharing options...
Delfins Posted June 20, 2008 Report Share Posted June 20, 2008 Ar RowNum nebūtu vienkāršāk? (nezinu vai pie updeita tas ir pieejams) update tabula set vieta = @rownum order by rand() Link to comment Share on other sites More sharing options...
shurix Posted June 20, 2008 Author Report Share Posted June 20, 2008 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 Link to comment Share on other sites More sharing options...
Aleksejs Posted June 20, 2008 Report Share Posted June 20, 2008 Pamēģini pārvietot "LIMIT 1"daļu no subquery uz galveno query, manuprāt, rezultātam navajadzētu mainīties. Link to comment Share on other sites More sharing options...
bubu Posted June 20, 2008 Report Share Posted June 20, 2008 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. Link to comment Share on other sites More sharing options...
Alnis Posted June 20, 2008 Report Share Posted June 20, 2008 MySQL versiju var apskatīties šitā: select version(); Link to comment Share on other sites More sharing options...
shurix Posted June 21, 2008 Author Report Share Posted June 21, 2008 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()); Link to comment Share on other sites More sharing options...
Recommended Posts