Doctrine2子查询 [英] Doctrine2 subquery
问题描述
我正在尝试在doctrine2中编写一个子查询,以便按另一个表的date列对一个表进行排序。
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
这个错误是正确的,但是如果doctrine2中不允许使用LIMIT以及当我尝试使用querybuilder(我的意思是子查询)来执行此操作时,如何将查询限制为1行使用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?
谢谢
推荐答案
使查询准确返回一排。尝试从B b那里选择MAX(b.date)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屋!