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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料