![]() |
select whit random? help :) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: select whit random? help :) (/showthread.php?tid=23552) |
select whit random? help :) - El Forum - 10-14-2009 [eluser]tnathos[/eluser] Hi i have the next problem. I have a table whit the next rows. id, name the id field, is an autoincrement and have 10 rows.. so i have 1, 2, 3,..10 in this table. I need select only 3 rows, but i need select in mode random the rows for example id =2, id=7, id=10, and if again run the select id=4,id=1,id=8.. etc. for the random i include the sentece select into (for) and send the parameter id using a variable random 1 to 10.. but how i cant get the result and put in a structure for union the others results and retunr to the view? thanks for the advance select whit random? help :) - El Forum - 10-14-2009 [eluser]davidbehler[/eluser] Try Code: select * from table_name order by rand() limit 0, 3 That would return 3 random rows from your table select whit random? help :) - El Forum - 10-14-2009 [eluser]tnathos[/eluser] [quote author="waldmeister" date="1255548678"]Try Code: select * from table_name order by rand() limit 0, 3 That would return 3 random rows from your table[/quote] thanks!! select whit random? help :) - El Forum - 10-14-2009 [eluser]jedd[/eluser] Side-note : using random sort order is expensive, especially on big tables. Typically it will be faster to calculate the number of rows you have in the table, use a PHP function to calculate n random numbers within that range, and modify your SELECT to use a 'WHERE IN ( ... )' construct. Yes, I do note you say ten items will exist in this particular table, but it's not clear if that's an immutable set of data. |