学说2,我该如何订购?

时间:2011-03-30 17:12:56

标签: sql-order-by doctrine-orm

我如何在学说2中订购?我甚至不能做近似..

表:

book
----
id
title

vote
----
id
book_id
value

author
----
id
name

book_author
----
book_id
author_id

我所拥有的是来自作者的身份证明,所以我必须退回通过投票订购的书籍。订购类似这样的ORDER BY(SUM(v.value)/ COUNT(v))和GROUP BY v.book_id ..但我不知道如何在表之间进行所有连接..例如,一个简单的连接返回作者的书籍将是这样的:

SELECT b FROM Entity \ Book b JOIN b.authors a WHERE a.id = {$ author-> id}

..但我怎么能在这里投票?一些建议?

干杯

1 个答案:

答案 0 :(得分:1)

嗯..最后我解决了这样的SQL:

$sql = "SELECT b2.*, b1.mark FROM (SELECT book_id, SUM(v.value) / COUNT(*) as mark FROM vote v GROUP BY v.book_id) as b1" .
                  " RIGHT JOIN" .
                  " (SELECT * FROM author_book ba JOIN book b ON ba.book_id=b.id WHERE ba.author_id = {$author->id}) as b2 ON b2.id=b1.book_id ORDER BY b1.mark DESC";

                  $rsm = new \Doctrine\ORM\Query\ResultSetMapping;
                  $rsm->addEntityResult('Project\Entity\Book', 'b');
                  $rsm->addFieldResult('b', 'id', 'id');
                  $rsm->addFieldResult('b', 'isbn', 'isbn');
                  $rsm->addFieldResult('b', 'title', 'title');
                  $rsm->addFieldResult('b', 'summary', 'summary');
                  $rsm->addFieldResult('b', 'pages', 'pages');
                  $rsm->addFieldResult('b', 'price', 'price');
                  $rsm->addFieldResult('b', 'published_at', 'published_at');

也许对某人有用,欢呼。