如何随机给出mysql查询的限制?

时间:2021-05-29 15:39:53

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

我在mysql中有一个select查询。在每次执行这个查询时,我需要不同数量的数据,在10到30个条目之间。

I tried with following code :

我尝试了以下代码:

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

But it's always giving result of limit 30.

但它总是给出极限30的结果。

How can i execute it?

我如何执行它?

5 个解决方案

#1


0  

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

极限x,y说你会得到30的结果从位置10。

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

尝试$limit = rand(10,30) ->…限制,限制美元。”

#2


2  

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

限制10,30意味着它将显示30个记录,从记录编号开始。10。所以有什么问题。

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

如果你想要从10到30的记录,那就用10,20。

#3


2  

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

如果你想要10到30个结果,你可以用PHP来构建你的查询。

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

$sql = "SELECT * FROM user where status='0' LIMIT "。兰特(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

这将给出相同的结果顺序每次只是不同的量。如果你真的想要一个随机结果集你可以用MySQL随机化顺序。

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

$sql = "SELECT * FROM user where status='0' ORDER BY RAND()限制"。兰特(10、30);

#4


0  

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

如果你期望的结果是20,也许你想…

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

Its essentially LIMIT [offset, amount]

其本质上是限制[偏移量]

#5


0  

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

限制10,30将给你记录编号10和记录将是30,

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

从美元= ' 2 ';美元记录=“10”;选择* FROM user, where status='0' LIMIT $ FROM,$records。

#1


0  

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

极限x,y说你会得到30的结果从位置10。

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

尝试$limit = rand(10,30) ->…限制,限制美元。”

#2


2  

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

限制10,30意味着它将显示30个记录,从记录编号开始。10。所以有什么问题。

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

如果你想要从10到30的记录,那就用10,20。

#3


2  

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

如果你想要10到30个结果,你可以用PHP来构建你的查询。

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

$sql = "SELECT * FROM user where status='0' LIMIT "。兰特(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

这将给出相同的结果顺序每次只是不同的量。如果你真的想要一个随机结果集你可以用MySQL随机化顺序。

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

$sql = "SELECT * FROM user where status='0' ORDER BY RAND()限制"。兰特(10、30);

#4


0  

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

如果你期望的结果是20,也许你想…

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

Its essentially LIMIT [offset, amount]

其本质上是限制[偏移量]

#5


0  

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

限制10,30将给你记录编号10和记录将是30,

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

从美元= ' 2 ';美元记录=“10”;选择* FROM user, where status='0' LIMIT $ FROM,$records。