[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

mysql query for sample from table

I need a mysql query to extract a sample of n rows from a table with r records.
0
robrodp
Asked:
robrodp
  • 3
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The SQL Server 2012 answer would be TABLESAMPLE.  Don't know if this works in MySQL.
http://msdn.microsoft.com/en-us/library/ms189108(v=sql.105).aspx

use adventureworks2012
go

SELECT *
FROM Person.Address
TABLESAMPLE (500 ROWS) 
GO

Open in new window

0
 
robrodpAuthor Commented:
I need it for MySql
0
 
Dave BaldwinFixer of ProblemsCommented:
Here's the basic version.

$nrows = 22;  // example value for rows
$sql = "SELECT * FROM yourtable LIMIT $nrows";

Open in new window


This page http://dev.mysql.com/doc/refman/5.1/en/select.html shows all the variations for a SELECT statement.
0
 
robrodpAuthor Commented:
Bit that wont get me a random sample right?
0
 
robrodpAuthor Commented:
I got it. Sorry I did not state the random part inmy question.

SELECT * FROM table ORDER BY RAND() LIMIT 10000
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now