Micku
Micku

Reputation: 550

How to give limit in mysql query in random

I have a select query in mysql . On each execution of this query I need different amount of data, between 10 and 30 entries.

I tried with following code :

'SELECT * FROM user where status='0' LIMIT 10,30'

But it's always giving result of limit 30.

How can i execute it?

Upvotes: 1

Views: 2014

Answers (5)

D-Rock
D-Rock

Reputation: 2676

If you want between 10 and 30 results and you are building your query in PHP you can use

$sql = "SELECT * FROM user where status='0' LIMIT " . rand(10,30);

This will give the same order of results each time however just a different amount. If you really want a random result set you can randomize the order with MySQL

$sql = "SELECT * FROM user where status='0' ORDER BY RAND() LIMIT " . rand(10,30);

Upvotes: 2

Khurram
Khurram

Reputation: 711

limit 10,30 will give you records from record umber 10 and records will be 30 in total,

so if u want to change it in every query u might need to pass limit parameter for example

$from = '2'; $records = '10'; SELECT * FROM user where status='0' LIMIT $from,$records

Upvotes: 0

Sindhara
Sindhara

Reputation: 1473

LIMIT x,y says that you'll get 30 results up from position 10

try $limit = rand(10,30) --> ... LIMIT '.$limit.'

Upvotes: 0

Lee Davis
Lee Davis

Reputation: 4756

If your expecting a result limit of 20, maybe you want..

'SELECT * FROM user where status='0' LIMIT 10,20'

Its essentially LIMIT [offset, amount]

Upvotes: 0

user1432124
user1432124

Reputation:

Limit 10,30 means it will show 30 record starting from record no. 10.So what's the problem.

If you want record from 10 to 30 then use limit 10,20

Upvotes: 2

Related Questions