我需要使用原則執行sql查詢。查詢是非常簡單的學說查詢幫助
選擇Q *,COUNT(a.question_id)FROM 問題爲Q LEFT JOIN question_answers AS一個ON q.id = a.question_id GROUP BY a.question_id
爲此我寫了下面的代碼
$query = Doctrine_Query::create()
->select('q.*, COUNT(a.question_id) AS answers')
->from('Model_Question q')
->leftJoin('Model_QuestionAnswers a ON q.id = a.question_id')
->groupBy('a.question_id');
但是,這並不讓我所需的查詢。任何人都可以解釋我缺少的東西嗎?所生成我通過$query->buildSqlQuery()
檢查查詢
SELECT q.id AS q__id, q.title AS q__title, q.short_description AS q__short_description,
q.created_at AS q__created_at, q.updated_at AS q__updated_at, q.is_visible AS
q__is_visible, q.category_id AS q__category_id, q.user_id AS q__user_id, q.category_name
AS q__category_name, q.username AS q__username, COUNT(q2.question_id) AS q2__0 FROM
question q, question_answers q2 GROUP BY q2.question_id
如果從連接中刪除「ON」條件會發生什麼(這應該是從模型判斷的默認條件,因此沒有理由指定它 - 它會自動連接到這些列上)? – prodigitalson 2011-04-27 08:27:07