简体   繁体   English

MySql中的快速随机行

[英]Fast random row in MySql

I need to generate a random row from my MySql database and i have found example here: http://akinas.com/pages/en/blog/mysql_random_row/ 我需要从我的MySql数据库生成一个随机行,我在这里找到了一个示例: http//akinas.com/pages/en/blog/mysql_random_row/

And i want to use solution 3 which looks like this: 我想使用如下所示的解决方案3:

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `users` LIMIT $offset, 1 " );

My code looks like this now: 我的代码现在看起来像这样:

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( "SELECT * FROM `users` WHERE profile_image='2' LIMIT $offset, 1 " );
$random_date = mysql_fetch_array($result);

echo $random_date['user_name']; //display username of random user

But when i refresh the page: approximatly 7 of 10 times nonthing shows up. 但是当我刷新页面时:大约有10次中有7次出现。 No username at all and also im trying to print out the id of the user but it's also empty. 没有用户名,我也试图打印出用户的ID,但它也是空的。 It seems that it's not getting anything at all from the database when refreshing and sometimes it get's data from the database. 它似乎在刷新时从数据库中得不到任何东西,有时它从数据库中获取数据。 Any idea why this might happen? 知道为什么会这样吗?

In this particular case, the problem is that you're working with two different queries; 在这种特殊情况下,问题是您正在处理两个不同的查询; the first query is: 第一个查询是:

SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users`

Whereas the second one includes a condition: 而第二个包括一个条件:

SELECT * FROM `users` WHERE profile_image='2' LIMIT $offset, 1
                      ^^^^^^^^^^^^^^^^^^^^^^^

Both queries should operate over the same result set. 两个查询都应该在相同的结果集上运行。

Its because in your first query you dont have a where, but in the second query you do. 这是因为在你的第一个查询中你没有位置,但在第二个查询中你做了。

if you have 50 rows and only 15 with profile_image = 2 That will be the reason why most appear as nothing. 如果你有50行,只有15行有profile_image = 2这就是为什么大多数都没有出现的原因。

Your query becomes LIMIT 30,1 when there are only 15 results for example LIMIT 30,1当只有15个结果时,您的查询变为LIMIT 30,1

Make sure the same where is used in both queries. 确保在两个查询中使用相同的位置。

Also avoid using mysql_* functions in new code 还要避免在新代码中使用mysql_ *函数

The solution for my problem was this, BIG thanks to: @Jack 我的问题的解决方案就是这个,非常感谢:@Jack

This query will find random row in a mysql table very fast. 这个查询会很快找到mysql表中的随机行。

$gender_search =  $logged_in_user['gender_search'];

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE profile_image='2' AND gender='$gender_search'");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( "SELECT * FROM `users` WHERE profile_image='2' AND gender='$gender_search' LIMIT $offset, 1 " );
$random_date = mysql_fetch_array($result);

I have now converted this string to PDO if someone needs it. 如果有人需要,我现在已将此字符串转换为PDO。

$statement = $dbConn->prepare("SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE profile_image=? AND gender=?");
$statement->execute(array('2',$logged_in_user['gender_search']));
$offset_row = $statement->fetch(PDO::FETCH_OBJ);
$offset = $offset_row->offset;

$statement2 = $dbConn->prepare("SELECT * FROM `users` WHERE profile_image=? AND gender=? LIMIT ?, 1");
$statement2->execute(array('2', $logged_in_user['gender_search'], $offset));
$random_date = $statement2->fetch(PDO::FETCH_BOTH);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM