close

一個 table 對另兩個 table 皆為一對多的關係。如果在一個查詢裡,要算出主表在兩個副表中的筆數,語法如下:

@hot_tags = Tag.find(:all,
:select => "tags.*, count(question_tags.id) as tags_q_count, + count(group_tags.id) as tags_g_count, (count(question_tags.id) + count(group_tags.id)) as tags_count",
:group => "question_tags.tag_id",
:joins => "left join question_tags on question_tags.tag_id = tags.id left join group_tags on group_tags.tag_id = tags.id",
:order => "tags_count desc",
:limit => 10)


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 cactis 的頭像
    cactis

    當鞋子合腳時....

    cactis 發表在 痞客邦 留言(0) 人氣()