简体   繁体   English

如何使用PHP从Mysql数据库中选择随机列值

[英]How select random column value from Mysql database using PHP

I'm trying to fetch random column from database using Rand() function. 我正在尝试使用Rand()函数从数据库中获取随机列。 It's returning random value but many time it's returning duplicate. 它返回随机值,但很多时候返回重复值。 This is what my database table look like. 这是我的数据库表的样子。

|------
|Column|Type|Null|Default
|------
|no|int(30)|No|
|postid|varchar(100)|Yes|NULL
|byuser|varchar(32)|Yes|NULL
|likeslimit|int(30)|No|
|createdon|date|No|
|-----

And this is what my PHP code is. 这就是我的PHP代码。

$query = mysqli_query($mysql, "SELECT postid FROM history ORDER BY Rand() Limit 1");
    if (mysqli_num_rows($query) == 1) {
        while ($row = mysqli_fetch_assoc($query)) {
            echo $row['postid'];
        }
    }

I want it to always return random never the same till the end of data reached. 我希望它始终返回随机数,直到数据结束为止。

don't use loop and condition you want only 1 limit try this 不要使用循环和条件,您只需要1个限制就可以尝试

 $query = mysqli_query($mysql, "SELECT postid FROM history ORDER BY Rand() Limit 1");

        $row = mysqli_fetch_assoc($query)
        echo $row['postid'];

This is the way RAND in mysql works and will repeat the results from time to time. 这是mysql中RAND的工作方式,并会不时重复结果。 But you can achieve such functionality by using mysql with php. 但是您可以通过将mysql与php结合使用来实现这种功能。

$query = mysqli_query($mysql, "SELECT postid FROM cacheTable WHERE 1 ORDER BY RAND() LIMIT 1");
$row = mysqli_fetch_assoc($query);
$foundId = (int)$row['postid'];

if((int) $foundId === 0) { // NO records left in cacheTable then fill it up again
    mysqli_query($mysql, "INSERT INTO cacheTable (postid) SELECT postid FROM history");
    $query = mysqli_query($mysql, "SELECT postid FROM cacheTable WHERE 1 ORDER BY RAND() LIMIT 1");
    $row = mysqli_fetch_assoc($query);
    $foundId = (int) $row['postid'];
}

mysqli_query($mysql, "DELETE FROM cacheTable WHERE postid=".$foundId); // DELETE the record

$query = mysqli_query($mysql, "SELECT * FROM history WHERE postid=".$foundId);
$result = mysqli_fetch_assoc($query);

cacheTable will have only one column - ID (primary key) which will hold the corresponding ID (primary key) from history. cacheTable将只有一列-ID(主键),它将保存历史记录中的相应ID(主键)。 cacheTable structure: cacheTable结构:

|------
|Column|Type|Null|Default
|------
|postid|varchar(100)|Yes|NULL
|-----

cacheTable will fill with all the ids from history table (it will be done once the cacheTable is empty). cacheTable将填充history表中的所有ID(一旦cacheTable为空,就会完成此cacheTable )。 You will select rand result from the cacheTable and you will delete it then so it will not appear in the next selects. 您将从cacheTable选择rand结果,然后将其删除,这样它就不会出现在下一个选择中。 When we are out of records in cache table it will populate again. 当我们的缓存表中的记录不足时,它将再次填充。

NB: this approach has one major drawback - when you have new entries in history table they won't be available in cache table until it is empty and filled again. 注意:这种方法有一个主要缺点-当您在history表中有新条目时,它们将在缓存表中不可用,直到它为空并再次填充。

This is the code Samir Nabil Suggested : 这是Samir Nabil建议的代码:

session_start();
$_SESSION['dupes'] = array();
$query = mysqli_query($mysql, "SELECT postid FROM history ORDER BY Rand() Limit 1");
    if (mysqli_num_rows($query) == 1) {
        while ($row = mysqli_fetch_assoc($query)) {
            if (!in_array($row['postid'],$_SESSION['dupes'])){
                 echo $row['postid'];
                 $_SESSION['dupes'][] = $row['postid'];
            }
        }
    }

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

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