社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

为什么我的MySQL程序应该选择一个随机行,而只选择三个特定值中的一个?

just_another_question • 3 年前 • 1285 次点击  

我有一个名为 quote_of_the_day 它应该从列表中选择不同的行 quotes 表中的日期作为种子值。当我将代码作为查询进行测试时,一切似乎都运行良好,但当我在php代码中调用函数时(查询是 CALL quote_of_the_day() )它只会从三个不同的引号中选择一个,尽管表中有23个引号。

创建systax for 引用今天的话 是:

DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `quote_of_the_day`()
BEGIN
        # save the number of rows into a variable
        SET @num_rows = (SELECT COUNT(*) FROM `quotes`);

        # calculate a random number no greater than the number of rows in the table
        SET @rand_num = (SELECT FLOOR(RAND(CURDATE())*(@num_rows+1)));

        # select random quote from the list of quotes (seed value is the current day)
        SELECT quote FROM quotes WHERE id = @rand_num;
        
        # increment the quoted column to keep track of which quotes are selected
        UPDATE quotes SET quoted = quoted + 1 WHERE id = @rand_num AND last_used <> CURDATE();
        UPDATE quotes SET last_used = CURDATE() WHERE id = @rand_num;
    END;;
DELIMITER ;

我哪里做错了?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/127950
 
1285 次点击  
文章 [ 2 ]  |  最新文章 3 年前