我想要把题库设计一下翻页功能,Mysql语句也很简单,在本地测试一点问题都没有,但是一放到服务器上便提示内存资源耗竭,最后在查看了一些资料后,在后面加了一个limit 1,如此便跑得飞快:

实现代码:

    public static function PrevQuestion($id,$uid=null,$testpaper=null)
    {
        $query = Question::find()->where(['<','id',$id]);

        if($uid)
        {
            $query->andWhere(['author'=>$uid]);
        }

        if($paper)
        {
            $query->LeftJoin('sq_testpaperinfo t','t.paper_id='.$paper_id);
        }

        $query->orderBy('id DESC');

        $query->limit(1);

        return $query->one();
    }

    public static function NextQuestion($id,$uid=null,$testpaper=null)
    {
        $query = Question::find()->where(['>','id',$id]);

        if($uid)
        {
            $query->andWhere(['author'=>$uid]);
        }

        if($paper)
        {
            $query->LeftJoin('sq_testpaperinfo t','t.paper_id='.$paper_id);
        }

        $query->orderBy('id DESC');
        $query->limit(1);

        return $query->one();
    }

下面是参考内容:

/**
* 获取当前记录的上一条记录
* @param seq
* @return
*/
@Select("select title, random_code from tb_blog where seq < ${seq} order by seq desc limit 1 ")
public Map<String,Object> getPreviousBlog(@Param("seq") long seq);

/**
* 获取当前记录的下一条记录
* @param seq
* @return
*/
@Select("select title, random_code from tb_blog where seq > ${seq} order by seq asc limit 1 ")
public Map<String,Object> getPostBlog(@Param("seq") long seq);

seq 是自增序列,random_code是id,title是博客标题。

seq不是主键,要使seq自增,先添加seq字段或建表时创建,再将seq添加unique索引,再使seq auto_increment,因为自增长列必须先是unique key或primary key。

Leave A Comment

Recommended Posts

在原文件名前加上前缀并实现文件的批量重命名

要在原文件名前加上前缀并实现文件的批量重命名,你可以根据你所使用的操作系统选择合适的脚本语言来编写脚本。以下是在不同操作系统上实现这一功能的示例: 在 Windows 上使用 PowerShell 在 macOS/Linux 上使用 Shell 脚本 在 Python 中使用 os 模块 注意事项 将上述脚本中的 folder_path 或 $folderPath 替换为你的文件夹路径,将 prefix 替换为你想要添加的前缀,然后运行脚本即可。

blueidea