views:

112

answers:

2

I have a script that has a GET variable: $_GET['percentage']

I have a MySQL table of data.

Now lets say that there are 100 rows of data in this table.

In pseudo-code:

SELECT data FROM table

Now would it be possible to select $_GET['percentage'] of random data from table?

For example (again in pseudo-code):

$_GET['percentage'] = 10;
SELECT 10% of data from table order by rand()

If this IS possible, how could I do it?

+1  A: 

If you have auto incremented ID field you may use

HAVING ID_FIELD<=ceil(count(*)*10/100);

Otherwise a stored procedure can help in this.

kv
+7  A: 

In MySQL, it's probably easiest to do this in two queries. First, get the count of rows in the table:

SELECT COUNT(*) FROM MyTable;

Then prepare the query to get random rows:

SELECT ... FROM MyTable ORDER BY RAND() LIMIT ?;

Then execute the prepared query and send the value of the count divided by 10.

Not every problem needs to be solved by a single query.


Here's an example PHP script, edited to use the old mysql extension.

<?php

// Get the total number of rows in the table.
$sql = "SELECT COUNT(*) FROM Kingdoms";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$rows_in_table = $row[0];

// We only want a portion of the rows, specified by the user
// choice of percentage.  The count we want is therefore equal
// to the total number of rows in the table multiplied by the
// desired percentage.
$percentage = intval($_GET["percentage"]) / 100.0;
$count = intval(round($rows_in_table * $percentage));

// LIMIT makes the query return at most the number of rows specified.
// Sort randomly first (if the table has too many rows this will be slow),
// then return the first $count rows.
$sql = "SELECT * FROM Kingdoms ORDER BY RAND() LIMIT {$count}";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
  print_r($row);
}

PS: Always be careful when interpolating a variable into an SQL expression. You should force the variable to a known format -- an integer value in this case. Otherwise you risk creating an SQL Injection vulnerability.

Bill Karwin
+1 "Not every problem needs to be solved by a single query." :)
jensgram
My apologies, I'm *really* new with MySQL. How would I implement this *exactly*? And also, would it be possible to get a percentage of a percentage? For example, `$_GET['percentage'] of 33%`
Rob
Thank you, but I'm not using PDO, and it looks very confusing. Sorry for bothering, but can you give me another example, without PDO? And possibly comments, if its this confusing.
Rob
I rewrote the example using the old mysql extension. But I do recommend learning how to use mysqli or PDO.
Bill Karwin
Thanks a lot, and yeah, I'll look into mysqli and PDO.
Rob
Oh, and one more thing. Say I wanted a static percentage of 33% of the rows, and then the user gets to select a percentage of that. Would it `$percentage = intval($_GET["percentage"]) / 300.0;` instead?
Rob
Yes, that would give the result you describe, but I prefer to keep the factors separate, in case you want to change them in the future. You could even define the 33% figure in a `constant`.
Bill Karwin
I see. Yeah, I'll probably set a variable in some configuration include somewhere to change it. Thanks for all the help
Rob