Reham Fahmy
Reham Fahmy

Reputation: 5073

MYSQL Limit Math

Let say i've database table (my_table) where i'm going to store id's

Now i want to divide the count of id's into 3 parts say x,y,z

then calling it 3 times each time with limit from/to

1 to x
x+1 to y
y+1 to z

Example :

$sql = "SELECT COUNT(id) AS allids FROM my_table";
$cus = mysql_query($sql) or die($sql);
$cuss = mysql_fetch_array($cus);

Now $z = $cuss['allids']; // All

I want to say

$qry="select * from my_table where id='". rand(1,$x)."'";

and

$qry="select * from my_table where id='". rand($x+1,$y)."'";

and

$qry="select * from my_table where id='". rand($y+1,$z)."'";

Got the idea, i'm going to call it in certain range

Mathematical Problem

I do not know how exactly how is the total id's also it could be even or add so i need the important is each one does not show results conflict with the other calling.

Any idea

Example : let say the total id's = 9 then i'll divide it on 3 so

X = 9/3 = 3
z = 9 (all)
y = z - x = 9 - 3 = 6

then we can get

rand (1,x) = (1,3)
rand (x+3,y) = (4,6)
rand (y+1,z) = (7,9)

What if the total id's was even or odd ?!

Upvotes: 0

Views: 126

Answers (1)

Nedret Recep
Nedret Recep

Reputation: 728

What about:

SELECT * FROM my_table WHERE id BETWEEN x and y ORDER BY RAND() LIMIT 1

Upvotes: 1

Related Questions