1054 - Unknown column in subquery
Try this

SELECT a.descendant_id
    , CASE a.path_length WHEN 0 THEN null ELSE a.ancestor_id END ancestor_id
    , CASE a.path_length WHEN 0 THEN null ELSE a.path_length END path_length
FROM webineh_prefix_nodes_paths AS a 
JOIN (
    SELECT descendant_id, MIN(CASE path_length WHEN 0 THEN 1000 ELSE path_length END) min_path
    FROM (
        SELECT a.* 
        FROM (
            SELECT userid  
            FROM webineh_user_buys 
            GROUP BY userid
            HAVING SUM(amount) &gt;= 1000
            ) AS buys_d 
        JOIN
            webineh_prefix_nodes_paths AS a 
        ON a.descendant_id = buys_d.userid
        JOIN (
            SELECT userid  
            FROM webineh_user_buys 
            GROUP BY userid
            HAVING SUM(amount) &gt;= 1000
            ) AS buys_a 
        ON a.ancestor_id = buys_a.userid 
        ) tmp2
    GROUP BY descendant_id 
    ) td
ON a.descendant_id = td.descendant_id
       AND a.path_length = CASE td.min_path WHEN 1000 THEN 0 ELSE td.min_path END ;