Doctrine2 子查询 [英] Doctrine2 subquery

查看:34
本文介绍了Doctrine2 子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在学说 2 中编写一个子查询,以对按另一个表的日期列排序的表进行排序.

I'm trying to write a subquery in doctrine2, to sort a table ordered by date column of another table.

(假设我在子查询 b.a_id = a.id 中查询表 A,它有一个 id 列,B 有一个 a_id 和一个日期)

(let's say I'm querying table A, which has an id column, and B has an a_id and a date, in the subquery b.a_id = a.id)

我正在使用查询生成器和 addSelect 方法,但由于我无法在查询中使用 LIMIT,因此出现此错误:

I'm using query builder, and the addSelect method, but since I can't use LIMIT in my query I get this error:

SQLSTATE[21000]:基数违规:1242 子查询返回更多超过 1 行

SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row

这个错误是真的,但是我怎么能把这个查询限制在 1 行,如果在学说 2 中不允许 LIMIT 并且当我尝试通过 querybuilder 执行它(我的意思是子查询)并且我正在使用 setMaxResults,然后getDQl 还是不行.

This error is true, but how could I limit this query to 1 row, if LIMIT is not allowed in doctrine2 and when I try to do it by querybuilder (I mean the subquery) and I'm using setMaxResults, and then getDQl it is still not working.

->addSelect('(SELECT b.date FROM B b WHERE b.conversation = a.id ORDER BY b.date DESC)')

我的问题有什么解决办法吗?

Is there any solution for my problem?

谢谢

推荐答案

让查询只返回一行.试试 SELECT MAX(b.date) FROM B b WHERE b.conversation = a.id)

Make the query return exactly one row. Try SELECT MAX(b.date) FROM B b WHERE b.conversation = a.id)

这篇关于Doctrine2 子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆