• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
select whit random? help :)

#1
[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

#2
[eluser]davidbehler[/eluser]
Try
Code:
select * from table_name order by rand() limit 0, 3

That would return 3 random rows from your table

#3
[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!!

#4
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.