dqd72925 2016-03-04 14:54
浏览 24
已采纳

选择上一个值学说查询

My table looks something like this

id  |  name   | revision_number | form_key | locked
---------------------------------------------------
1   |  Name1  |        1        |   abcd   |  true
2   |  Name2  |        1        |   efgh   |  false
3   |  Name1  |        2        |   abcd   |  true
4   |  Name2  |        2        |   efgh   |  true
5   |  Name2  |        3        |   efgh   |  true

Now I want to get the forms that were locked last, bases on form_key, so in this case the one with id = 3 and name = Name1 and the one with id = 5 and name = Name2

This is the query I currently have

$revisionQuery =$em->getRepository('AppBundle:Form')->createQueryBuilder('f')
->select('f')

->where('f.lockBit = :locked')

->andWhere('f.revisionNumber = (SELECT MAX(f2.revisionNumber) FROM AppBundle:Form f2 WHERE f.formKey = f2.formKey GROUP BY f2.formKey)')

->setParameters(['locked' => true]);

But this doesn't give me the results I want.

  • 写回答

1条回答 默认 最新

  • dongquxiao8545 2016-03-04 18:59
    关注

    I give to you the SQL for do it. I think you are going to need a Native DQL for Doctrine

    SELECT
        f.id,
        f.`name`,
        f.revision_number,
        f.form_key
    FROM
        form f
    INNER JOIN (
        SELECT
            max(ff.revision_number) AS revision,
            ff.form_key
        FROM
            form ff
        GROUP BY
            ff.form_key
    ) tt ON f.form_key = tt.form_key
    AND f.revision_number = tt.revision
    

    Hope this help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加