I needed to have the ability to at random choose rows from the table in ways that scales as much as large tables in MySQL. When I comprehend it since really is not possible. Are there more databases which i could do that with?
Does this kind of selection are necessary with SQL? Otherwise, you can produce a random number in code after which obtain a related record through the produced number. Obviously that's creating a couple of presumptions:
- Would require that the Id's are a name seed
- your random number is bounded by min &lifier max values.
SELECT ColumnName FROM TableName ORDER BY RAND() LIMIT 1;
I believe you can do this... Sorry -- this really is TSQL, but FWIW
Declare @max int, @min int, @rand int set @max = (select max(id) from myTable) set @min = (select min(id) from myTable) set @rand = Cast(((@max + 1) - @min) * Rand() + @min as int) select * from myTable where id = @rand
Or if you have gaps inside your primary key fields, then:
select max(id) from myTable where id <= @rand
Per @andrew's objection -- he's concerned this would favor lower id amounts in not-matched up contests. If that is an issue, a few ways you can handle it:
Take 2 records at any given time (a min along with a max). For instance:
INSERT INTO whatever_table_variable_etc select max(id) from myTable where id <= @rand UNION select min(id) from myTable where id >= @rand
Or you might base it on odds and evens. For instance:
Declare @isOdd bool SET @isOdd= CASE WHEN ABS(@rand) % 2 = 1 THEN 1 ELSE 0 END If @isOdd = 1 select max(id) from myTable where id <= @rand ELSE select min(id) from myTable where id >= @rand