2012-01-04 113 views
0

有沒有辦法返回Mysql orderBy排名作爲結果記錄的一部分?Mysql:返回orderBy排名

例如,可以說我有一個意見表,其中我通過查詢結果排名:使用orderBy('r.comment_rating*r.comment_length')

comment_ratingcomment_length

現在我想要得到的記錄包括其各自的comment_rating*comment_length計算值。

這可能嗎?

編輯:另外,沒有教條執行排名計算兩次,如果我這樣做,也使用orderBy相同的算法?

回答

2

你的意思是這樣的:

SELECT *, (comment_rating * comment_length) AS ranking FROM comment ORDER BY ranking DESC 

編輯

沒有使用原則,但在documentation快速瀏覽後,我想這將是這樣的:

$q = Doctrine_Query::create() 
    ->select('*, (comment_rating * comment_length) AS ranking') 
    ->from('comment') 
    ->orderBy('ranking'); 
$comments = $q->execute(); 
+0

我將如何使用Doctrine做到這一點? – whamsicore 2012-01-04 16:11:03

+0

@whamsicore沒有使用Doctrine,但看看我更新的答案,並讓我知道它是否有效。 – 2012-01-04 16:17:58

+0

很好,謝謝! – whamsicore 2012-01-04 16:30:45

0

試試這個:

SELECT <yourFields>, (r.comment_rating * r.comment_length) AS Rating FROM ... 

文檔: Doctrine Query Language: Aggregate-values.

+0

是否學說進行排名計算兩次,如果我這樣做,也使用排序依據相同的算法? – whamsicore 2012-01-04 16:28:25

0

所有你需要做的是包括

(comment_rating*comment_length) as comment_ranking 

在SELECT字段列表中。

0
SELECT 
    comment_rating, 
    comment_length, 
    comment_rating*comment_length AS comment_rank 
FROM 
    tablename 
ORDER BY 
    comment_rank; 
1

試試這個:

Select comment_rating, comment_length, 
     (comment_rating * comment_length) as rat_len 
From comment 
OrderBy rat_len