2011-10-03 72 views
0

比方說,我有一個1:許多關係:獅身人面像:通過關聯的範圍排序

樹:蘋果

樹/蘋果各有一個主鍵ID列和蘋果有一定的日期屬性/列(created_at)。

使用獅身人面像,我想檢索所有的樹,按照給定時間段內創建的蘋果數量排序。因此,例如:

所有樹,按2010年1月1日至2011年1月1日之間創建的蘋果總數排序。

這可能嗎?

回答

0

所以,你有兩個表

create table tree (tree_id int unsigned primary key,...); 

create table apple (apple_id int unsigned primary key, tree_id int unsigned, created_at timestamp default current_timestamp,...); 

那麼接下來可以只建立在蘋果的指數

sql_query = select apple_id,tree_id,unix_timestamp(created_at) as created_at from apple 

然後通過查詢它

潤集團
$cl->setGroupBy('tree_id',SPH_GROUPBY_ATTR,'@count DESC'); 

@count虛擬屬性將爲您提供該樹上的蘋果數量。

,因爲你不使用它,可以設置排名爲無

$cl->setRankingMode(SPH_RANK_NONE); 

要明確設置過濾器

$cl->setFilterRange('created_at',strtotime('2010-01-01'),strtotime('2011-01-01')); 

而且你只需要使用一個空白查詢

$res = $cl->Query('',$index); 
+0

您count屬性沒有考慮日期範圍? – jsharpe

+0

是的,setFilterRange適用於setGroupBy – barryhunter