2012-04-08 89 views
1

每個childrow都有一個parentid和position。對於具有相同位置的孩子,有一行在start='1'mysql加入不正確的結果

我想要做的是返回掛起的行與他們的開始行。

應顯示的結果是;開始(歐文)待決(戴夫,保羅)。這是因爲他們的位置相同。這裏是SQL小提琴http://sqlfiddle.com/#!2/e6e54/1

id | name | parentid| position| start | pending | 
    1 | mike | 0 | 0  | 0 |  0 | 
    2 | dave | 1 | 1  | 0 |  1 | 
    3 | paul | 1 | 1  | 0 |  1 | 
    4 | john | 1 | 2  | 1 |  0 | 
    5 | bret | 1 | 2  | 0 |  0 | 
    6 | owen | 1 | 1  | 1 |  0 | 
    7 | rick | 1 | 3  | 1 |  0 | 
    8 | jaye | 1 | 3  | 0 |  0 | 



    $getquery = mysql_query("select child.* 
from `mytable` child inner join `mytable` parent 
on parent.id=child.parentid 
inner join `mytable` child2 on child.parentid=child2.parentid 
and child2.pending='1' 
where child.start='1' ORDER BY child.id DESC"); 



while($row=mysql_fetch_assoc($getquery)) { 

$name = $row['name']; 

echo "<p>Name: $name </p>"; 

} 
+0

您的查詢不使用'chid2'輸出。加入父項會消除根條目,因此您可能希望完全跳過它。 – dasblinkenlight 2012-04-08 15:19:22

+0

哦,我明白了..我將如何得到理想的結果?掛起的行和它們的起始行? – user892134 2012-04-08 15:26:39

+0

爲什麼只顯示owen,dave和paul?而不是所有的行? – 2012-04-08 15:31:57

回答

0
select * from `mytable` order by position, pending 

您可以添加where position=1條款,如果這是唯一感興趣的位置。