0

Consider this MySQL table:

Table 1

enter image description here

For MySQL versions that do not support Common Table Expressions (up to version 5.7), you could achieve this with the following query:

select  id,
        username,
        sponser_id
from    (select * from db_users
         order by sponser_id, id) products_sorted,
        (select @pv := '5') initialisation
where   find_in_set(sponser_id, @pv)
and     length(@pv := concat(@pv, ',', id));

I want to modify this query to perform inner join operations with another table.

Table 2 For table 2 you can take any example:

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Sonu Chohan
  • 141
  • 1
  • 5

1 Answers1

0

try this for recursion... reference:enter link description here

function recursiveCheck($src_arr, $parent_id=0, $tree = array())
{
    foreach($src_arr as $idx => $row)
    {
        if($row['parent_id'] == $parent_id)
        {
            foreach($row as $k => $v)
                $tree[$row['user_id']][$k] = $v;
            unset($src_arr[$idx]);
            $tree[$row['user_id']]['children'] = $this->recursiveCheck($src_arr, $row['user_id']);
        }
    }
    ksort($tree);
    return $tree;
}

$query = "Select * From tablename";

$result = mysqli_query($connect,$query);
$data = array();
while($row = mysqli_fetch_assoc($result)) {
    array_push($data,$row);
}


echo '<pre>';
print_r(recursiveCheck($data));
echo '</pre>';

and for inner join you can do

SELECT
select_list
FROM t1 INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;

check enter link description here

hammer
  • 82
  • 10