0

ユーザー名とリーダーの2列のテーブルがあります

  login_user    sponsered_id  right_left
    test1       admin          Right
    test2       admin          Left
    test3       test1          Right
    test4       test1          Left
    test43      test2          Left
    test44      test3          Left

機能があります

function display_children($parent, $level) { 

    // retrieve all children of $parent 

    $result = mysql_query('SELECT name, login_user, right_left FROM members_list '. 

                           'WHERE sponsered_id="'.$parent.'";'); 


    while ($row = mysql_fetch_array($result)) { 

        // indent and display the title of this child 


     echo '<tr><td>'.
   $row['login_user'].' </td><td> '.$row['right_left'].' </td><td> '.$row['sponsered_id'].
    "</td></tr>";  


        // call this function again to display this 

        // child's children 

        display_children($row['login_user'], $level+1); 

    } 

} 

echo display_children('admin',0);

しかし、正しい出力が得られない....それは私に出力を与える

test1   Right   admin
test3   Right   test1
test44  Left    test3
test4   Right   test1
test2   Left    admin
test43  Left    test2

として出力する必要があります

test1   Right   admin
test2   Left    admin

test3   Right   test1
test4   Left    test1

        Right   test2
test43  Left    test2

        Right   test3
test44  Left    test3
4

2 に答える 2

1

これを試して

SELECT 
    login_user,
    right_left, 
    sponsered_id  
FROM (
    SELECT 
        login_user,
        right_left, 
        sponsered_id  
    FROM 
        members_list

    UNION

    SELECT 
        NULL,
        CASE when max(right_left) ='Left' THEN 'Right' ELSE 'Left' END AS right_left, 
        sponsered_id
    FROM 
        members_list  
    GROUP BY 
        sponsered_id
    HAVING count(sponsered_id) < 2
) as temp
ORDER BY
    sponsered_id, 
    right_left
于 2013-03-24T17:57:42.147 に答える
0

あなたはこれを試すことができます

   select login_user , right_left , sponsered_id  from (

   select login_user , right_left , sponsered_id 
    from members_list
   union all

    select login_user , if(right_left ='left', 'right', 'right' ) right_left ,  sponsered_id
   from members_list 

  )t
  group by sponsered_id, right_left
  order by login_user

デモはこちら

于 2013-03-24T13:23:43.643 に答える