2011-10-05 105 views
0

我有查詢:MySQL的SELECT語句的結果限制

select id, name from categories where parent_id in (
    select id 
    from categories 
    where top_main_place > 0 
) 

它選擇兒童信息的父節點(內選)

問題的(外部選擇):我並不需要有所有子節點數據,最大值子節點數據每父母id

我該如何達到這個結果?

順便說一句,對不起,我英文不好

回答

2

您應該由外部查詢parent_id,給每一行一個行號(每當parent_id改變時重新設置行號),然後過濾出行號大於6的行,例如檢查this question和sql代碼。

+0

這是非常有用的,謝謝你,我現在正在讀.. – user973254

1
select id, name FROM (
    select id, name, IF(@parent_id=parent_id, @count:[email protected]+1, @count:=0) as count, @parent_id:=parent_id from categories, (select @count:=0, @parent_id:=0) as x where parent_id in (
     select id 
     from categories 
     where top_main_place > 0 
    ) order by parent_id 
) y where count <= 6; 
+0

貌似@nobody打我的建議,但這應該工作。 –

+0

你們倆幫了很多,謝謝! :) – user973254