dqp99585 2018-06-07 20:18
浏览 47
已采纳

PHP使用来自数据库的最新版本

I have a value in my database called revision which increases when there's a duplicate entry.

I'm trying to select the latest revision in my database. Example:

|-----------------------------|
| Title            | Revision |
|-----------------------------|
| Some title 1     | 0        |
| Some title 1     | 1        |
| Some title 1     | 2        | <-- select this one
|-----------------------------|

This is my query (just for example, I know the last argument will fail):

$titlecheck = $this->query("SELECT * FROM titles WHERE tmdb_name=:name AND tmdb_titleid=:skuid AND revision=:revision");
if($titlecheck->execute(array(":name" => $api[1]['tmdb']['name'], ":skuid" => $api[1]['tmdb']['titleid'], ':revision'=>LARGEST))) {
    // do something
}

How can I select the latest revision number? I have looked into MAX() but since I'm required to select all from the database I won't be able to use this.

  • 写回答

2条回答 默认 最新

  • dsxml2169 2018-06-07 20:22
    关注

    You need to use ORDER to ensure certain order of returned rows. Otherwise you may get it in random order:

    SELECT * FROM titles 
    WHERE tmdb_name=:name AND tmdb_titleid=:skuid
    ORDER BY revision DESC
    

    then the first row will be the one with highest Revision. If you need just that, use LIMIT 1 at the end of query and ensure revision column has index.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了